0

Variable variables in java? Am i misunderstanding array usage?

I've a list of ~50 describable objects. Each has its own array of possible descriptors. I'm trying to write code that iterates through the list, selects a random descriptor from the matching array, and stores into a final array for output. "list[i][]" represents a variable variable, _not_ a multidimensional array. Obvs doesn't work. Random r = new Random(); for (i<list.length){ int x = r.nextInt(list[i][].length); output[i] = list[i][x]; } Also, would a proper 2d array work here? And how?

7th Mar 2017, 9:06 AM
Winderine
Winderine - avatar
3 Respuestas
0
You make a wrong declaration of your multidimensional array. u must move between your list[i][j], not only list[i][]; an example: public void set(int i){ for (int j=0;j<list[0].length;j++){ list[i][j]=(int)(Math.random()*10+1); } /* list[0] is used for column of your multidimensional array as a matrix */ in your case: int x = r.nextInt(list.length);
7th Mar 2017, 9:17 AM
Vulcan Rider
Vulcan Rider - avatar
0
ok, now your for cycles are not good. for costruction is made by: (INITIALIZE_VARIABLE; CONDITION; INCREASE/DECREASE_VARIABLE) then this "databank[i][j].length" are wrong, doesnt exist, or .length or [0].length, or line or column. Do u want the number of all elements (line*column) or what? for(int i=0;i<databank.length;i++){ for(int j=0;j<databank[0].length;j++){ int x=r.nextInt(databank.length*databank[0].length); this line output[i] = databank[i][x]; for what? random numbers in the output array?
7th Mar 2017, 10:03 AM
Vulcan Rider
Vulcan Rider - avatar
0
I've posted a working 2d array program sort of solving this on my profile. Code critiques are welcomed there.
7th Mar 2017, 10:10 AM
Winderine
Winderine - avatar