0

I am having a method(function) that is supposed to add a (8*6)Array to an Array list. Function is called several times.

int [][] KeySelection = new int [8][7];

  for(int i=0;i<KeySelection.length;i++){
  for(int j=0;j<KeySelection[0].length;j++){
      KeySelection[i][j] = (int) (Math.random () * 2);

     }
  }

Each time this function is called a totally new KeySelection Array is done. What i need now is to declare an array list so that it could store all the KeySelection Arrays in that list.

So that when i need the first KeySelection Table i could find it in the first index of the Array list. Could somebody Help

2 Answers 2

2

Create an ArrayList of type int[][]: -

List<int[][]> list = new ArrayList<int[][]>();

And then, add your array to this list after creation: -

list.add(keySelection);

NOTE: - Variable name should start with lowercase letters.

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

7 Comments

OK perfect.. now Assume the list is full, when I need to call ,Lets say the first KeySelection Table,, what is the exact syntax
int[][] firstMatrix = list.get(0); Read the Java tutorial on collections and the List javadoc
One More question..Does the "(Math.random () * 2)" add the exact same values to the exact same locations.. so if the function runs twice to i get same KeySelection Tables
@user1111726.. What do you exactly want to add in those arrays?
I dont think I really get u there.. but what i am asking.. is that normal to have the two tables identical.. because thats what i wasnt and thats what is happening.. or is it just a conicedence
|
1

Since arrays in Java are special type of objects, you can use an ArrayList of objects to hold your arrays. Declare your list like that and populate it via for loop etc.

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

4 Comments

@user1111726 if you don't have such a class, so how your code uses this class ???
The code doesn't use a KeySelection class, but a variable named KeySelection.
I edited the question for better understanding.. the method intializes a new 2-D array called "KeySelection" every time the function is called, new values in the KeySelection table.. What i need is to add them to an array list each time the function is called so at the end I could have a list contating all Arrays of KeySelection
@JB Nizet.. Yes u got me.. is there a way to add those KeySelection Varaiable to the Arraylist each time the function is called

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.