I have a series of arrays in my Java program
Integer[] row1 = {0,0,0,0,0,0};
Integer[] row2 = {0,0,0,0,0,0};
Integer[] row3 = {0,0,0,0,0,0};
Integer[] row4 = {0,0,0,0,0,0};
Integer[] row5 = {0,0,0,0,0,0};
Integer[] row6 = {0,0,0,0,0,0};
I also have a randomly generated number between 1-6:
int selectrow = rand.nextInt(6)+1;
I need to refer to each of those rows based on the value of the generated number. So something like "row" + selectrow and then do something to it, but I am not sure how to achieve this without if statements. If statements would just get too messy. Any ideas?
Build an array of arrays.
And use int
, not Integer
, unless you have a very specific reason for using expensive objects.
And use loops to fill your matrix. Unless you fill it with 0
and use int
, because, well, it's the default value.
All in one, here's what your code could be like:
int[][] matrix = new int[6][6];
Random rand = new Random();
// there's probably something happening here
int[] selectedRow = matrix[rand.nextInt(6)];