0

I have a multidimensional array which was initialized integer values. I want to build ArrayList for all indexes(cells) in this array. Arraylists will take random numbers with a number of integer values in its corresponding cell. For example, multiarray[0][5] = 15 , I want to create an ArrayList which has the name of "0,5" (means 0 to 5) and add it 15 random values. I am trying to do this with for loop ;

for (int i=0; i<n; i++){
    for(int j=0; j<n; j++){
        if(i != j){
            String nameOfSenderBank=String.valueOf(i); 
            String nameOfReceiverBank=String.valueOf(j);
            ArrayList<Integer> ???  = new ArrayList<Integer>();
                for(int a=0; a<banksNumberOfOrders[i][j]; a++){
                    .....

                }
        }
    }
}

I can't name ArrayList dynamically with index numbers of the loop ,I'm typecasting Integer to String,but local variables aren't be used as ArrayList name,so how can I solve this problem.

1
  • 1
    In Java, variable declarations must be specified at compile time. You cannot dynamically create variables at runtime. Commented Dec 1, 2016 at 14:40

1 Answer 1

1

I think a better option is to have a HashMap<String, int[]> where the key would be the name 0 to 5 and the value would be the array of random values.

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

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.