简体   繁体   中英

How to index arrays inside an array list?

The Whole thing is I have a 2D Array :

int[][] Key = {{1,0,1,0,1,0},{1,0,1,0,1,0},{1,0,1,0,1,0},{1,0,1,0,1,0},
                        {1,0,1,0,1,0},{1,0,1,0,1,0},{1,0,1,0,1,0},{1,0,1,0,1,0}};

what is needed to split this 2D array in 8*1D array (1 for each block). so i defined 8 new 1D arrays as well as an array list to place all these Arrays in it :

   ArrayList<int[]> B_Blocks = new ArrayList<int[]>(); 

   int[] B0 = new int[Key[0].length];
   int[] B1 = new int[Key[0].length];
   int[] B2 = new int[Key[0].length];
   int[] B3 = new int[Key[0].length];
   int[] B4 = new int[Key[0].length];
   int[] B5 = new int[Key[0].length];
   int[] B6 = new int[Key[0].length];
   int[] B7 = new int[Key[0].length];

What i need is a some way to index the Arrays in side the array list, for example I want to add the first row values of Key to the first Array in the Arraylist

PS The whole point of the arraylist is to gain access easier later for more operations thru forloops for example. Any help Thanks

I tried using the following nested for loop to add values:

for (int i =0;i<Key.length;i++) {
       for (int j =0;j<Key[0].length;j++) {
       B_Blocks.get(i)[j] = Key[i][j] ;
        } 
   }

It runs but the values arent the same when I output:

for (int i =0;i<rightblock.length;i++) {
       for (int j =0;j<rightblockExp[0].length;j++) {
       System.out.print(B_Blocks.get(i)[j]);
        } System.out.print("\n");
   }

If you want to add an element at a specified index, you can use overloaded add(index, elem) method from list.

list.add(0, key[0][0]);
list.add(1, key[0][1]);

You can access the elements of an ArrayList by using list.get(i). For example:

int[] array1 = B_Blocks.get(0);

Hope that helps!

Perhaps we should come back to the original requirements:

what is needed to split this 2D array in 8*1D array (1 for each block)

and

What i need is a some way to index the Arrays in side the array list

You can work with 2D-arrays like this:

  • Key[0] will give you the first subarray (='row'), Key[1] the second, and so on.
  • Key[0][0] will give you the first element in the first subarray, Key[0][1] the second, and so on. Access to an element, lets say Key[0][5] , can also be formulated as

    int[] firstRow = Key[0]; doSomethingWith(firstRow[5]);

The first line lets the reference firstRow point to Key[0] , ie the first subarray. firstRow and Key[0] point to the same array - you seem to have thought the array gets copied when you stated:

No..no Why would i put them in a list if i am gonna get them back an place them in another array.

The ArrayList issue:

PS The whole point of the arraylist is to gain access easier later for more operations thru forloops for example. Any help Thanks

Nothing is going to get easier if you re-build that structure with ArrayLists - unless you want to add elements to the arrays respectively ArrayLists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM