-4
    int[] array = new int[9];
    int[][] matrix = {  {0,1,2},
                        {3,4,5},
                        {6,7,8}};
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            array[i+j] = matrix[i][i];
        }
    }
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }

So I expect the array to be set to {0,1,2,3,4,5,6,7,8,9}, but the out put is

00 01 02 43 44 45 86 87 88 

This is just a side project to solve a soduku puzzle, yet this problem has always stumped me. I usually try to find a way to go around this, but I've gotten to the point where I want to know why this isn't working.

braX
  • 11,506
  • 5
  • 20
  • 33
  • 3
    For starters, `matrix[i][i];` is almost certainly not what you meant to do, since it'll only grab the diagonal values. Also you're overwriting several of your target array values multiple times. – azurefrog Dec 11 '16 at 23:42
  • 2
    `array[i+j]` only reaches `array[4]`. How do you manage to get values into the rest of the slots? – RaminS Dec 11 '16 at 23:44
  • 1
    which sudoku is from [0 , 9] ? i always see the range [1 , 9 ] – Mohsen_Fatemi Dec 11 '16 at 23:45
  • @Mohsen_Fatemi It's [0,8] actually. – RaminS Dec 11 '16 at 23:48
  • To show you what you're doing wrong, add a `print` statement, so you can see what's happening. This is called [**debugging**](http://stackoverflow.com/q/25385173/5221149). See example at [IDEONE](https://ideone.com/k6aAfr). – Andreas Dec 11 '16 at 23:51
  • Based on the leading zero om the output, I would say that the real `matrix` and `array` are declared as `String[]` not `int[]`. – Stephen C Dec 11 '16 at 23:51
  • @StephenC Based on the fact that *that* code **cannot** produce *that* output (format *and* values), we can conclude only that OP is misrepresenting the output (or the code). – Andreas Dec 11 '16 at 23:52
  • Yup! I voted to close for that reason. Conrad, if you want us to help, show us the >>real<< code and not some phantasy version ... that doesn't exhibit your problem. – Stephen C Dec 12 '16 at 00:51
  • @Gendarme you're right – Mohsen_Fatemi Dec 12 '16 at 10:23

2 Answers2

3

I don't know how you're getting that output, that's not what your code gives. Anyway...

You don't want array[i+j], as that would give you values from 0 to 4, with repeats. You want to iterate through each element of array, so use array[i*3 + j]. Also, you don't want matrix[i][i], but matrix[i][j] instead, but I think that's just a typo.

Andreas
  • 154,647
  • 11
  • 152
  • 247
Iluvatar
  • 1,537
  • 12
  • 13
  • Thank you so much! This was bugging me a lot! Also I tried to change the code so it didn't involve a lot of needless variables I had set in the rest of the program. I must have messed up my output in doing so, but this fixed it :) – Conrad Melcher Dec 12 '16 at 00:24
0
 int index = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      array[index] = matrix[i][j];
      index++;
    }
  }
Anton Kim
  • 879
  • 13
  • 36