0

I am trying to input values from a 1d array into a 2d array in java.

This is what I have so far:

int[] input2 = {
    0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0
};
int[][] arr = new arr[3][4];

for (int i = 0; i < arr.length; i++) {
    for (int j = 0; j < arr[i].length; j++) {
        System.out.println("index" + ((i * arr.length) + j));
        arr[i][j] = input2[(i * arr.length) + j];
        //System.out.print("  " + arr[i][j]);

    }
    //System.out.println();
 }

But what it outputs is:

index0
index1
index2
index3
index3
index4
index5
index6
index6
index7
index8
index9

which means that I am getting the indexes wrong from 1d array. Where did I go wrong ?

6
  • Look at the index numbers. I have mentioned that I am trying to input values from 1d array to 2d array . Commented May 29, 2015 at 21:00
  • We don't understand what you want to put into the 2D array. Can you clarify if you want the index numbers and values and in which way you want them to appear? Commented May 29, 2015 at 21:02
  • @WillCampbell: I want values from 1d array to be put in 2d array using the indexes in 1d array. Commented May 29, 2015 at 21:03
  • @Pshemo: number of rows in 2d array = 3,n number of column = 4 in my cASE. You seem to have tried with R=3 AND C=3 Commented May 29, 2015 at 21:05
  • 1
    Can you add the exact dimensions of each of the arrays in your code? Commented May 29, 2015 at 21:05

6 Answers 6

2

Your mistake is that in each step you multiply by the number of rows rather than the number of columns.

If you want to get to the first element of the second row, you have to skip all the elements of the first row first. That would be 1 * arr[0].length. So your method may work in an X by X array, but not in an X by Y array.

Sign up to request clarification or add additional context in comments.

Comments

0

It could well be filling in 2 dimensions, but ((i*arr.length) + j) is probably just adding the values. I would add '+ ", "' in the middle of it to see what is actually getting output.

Comments

0

Try this in your loop -

((i*arr[0].length) + j) 

Comments

0

You should change your index formula from :

(i*arr.length) + j

to

(i*arr[i].length) + j

Comments

0

you can try this:

    int count = 0;
    for(int i=0; i< arr.length; i++)
     {
         for(int j=0; j<arr[i].length; j++)
         {
             //System.out.println("index" + ((i*arr.length) + j) );
             arr[i][j] = input2[count++];
             System.out.print("  " + arr[i][j]);

         }
    System.out.println();
     }

Comments

0

Try

int[] input2 = {0,1,0,0,0,1,1,0,1,0,1,0};
 int[][] arr = new arr[3][4];

for(int i=0; i< arr.length; i++)
         {
             for(int j=0; j<arr[i].length; j++)
             {
                 System.out.println("index" + ((i*arr[i].length) + j) );
                 arr[i][j] = input2[(i*(arr[i].length)) + j];
                 //System.out.print("  " + arr[i][j]);

             }
        //System.out.println();
         }

(Replaced ((i*arr.length) + j) with ((i*arr[i].length) + j)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.