Home>

I want to replace a 2D array that randomly stores numbers 1 to 6 with an ArrayList. What should I do?

I don't know how to replace a randomly stored 2D array with an ArrayList.

Corresponding source code
// Random storage in 2D array
for (int i = 0;i<5;i ++) {
 for (int j = 0;j<6;j ++) {
 array [i] [j] = (int) random (1,6);
 while (j>0&&array [i] [j] == array [i] [j -1])
 array [i] [j] = (int) random (1,6);
 while (i>0&&array [i] [j] == array [i -1] [j])
 array [i] [j] = (int) random (0,5);
// The While statement prevents the same numbers from being next to each other.
Supplementary information

Environment Processing 2.21
The corresponding image will be inserted in the number.

Postscript

Why I want to make it an ArrayList
In order to be able to replace the randomly stored numbers 1 to 6 by mouse operation, create a class and create an array containing numbers and images with ArrayList, and later operate it using dist etc. Is it easier to move than a two-dimensional array if it is in the form of doing? Because I thought.

It would be nice if it could be moved as a two-dimensional array, but I didn't know how to write it, so I came up with the idea of ​​making it an ArrayList.

  • Answer # 1

    Random in the question was a function of processing, wasn't it?

    Here is an example of rewriting an array into an ArrayList using Java's java.util.Random.

    import java.util.Random;
    import java.util.ArrayList;
    class Test {
        public static void main (String [] args) {
            Random random = new Random ();
            int [] [] array = new int [5] [6];
            for (int i = 0;i<5;i ++) {
                for (int j = 0;j<6;j ++) {
                    array [i] [j] = (int) random.nextInt (6) +1;
                    while (j>0&&array [i] [j] == array [i] [j -1])
                        array [i] [j] = (int) random.nextInt (6) +1;
                    while (i>0&&array [i] [j] == array [i -1] [j])
                        array [i] [j] = (int) random.nextInt (6) +1;
                }
            }
            for (int i = 0;i<5;i ++) {
                for (int j = 0;j<6;j ++)
                    System.out.print ("" + array [i] [j]);
                System.out.println ();
            }
        }
    }


    In ArrayList, the element is an Integer instead of an int.

    import java.util.Random;
    import java.util.ArrayList;
    class Test {
        public static void main (String [] args) {
            Random random = new Random ();
            ArrayList >array = new ArrayList<>();
            for (int i = 0;i<5;i ++) {
                array.add (new ArrayList<>());
                for (int j = 0;j<6;j ++) {
                    array.get (i) .add ((int) random.nextInt (6) +1);
                    while (j>0&&array.get (i) .get (j) == array.get (i) .get (j -1))
                        array.get (i) .set (j, (int) random.nextInt (6) +1);
                    while (i>0&&array.get (i) .get (j) == array.get (i -1) .get (j))
                        array.get (i) .set (j, (int) random.nextInt (6) +1);
                }
            }
            for (int i = 0;i<5;i ++) {
                for (int j = 0;j<6;j ++)
                    System.out.print ("" + array.get (i) .get (j));
                System.out.println ();
            }
        }
    }


    Postscript
    As you can see, if you use ArrayList instead of array,
    You have to write array [i] [j] as array.get (i) .get (j)
    Sometimes you have to write array [i] [j] = k as array.get (i) .set (j, k).
    Could you please add to the question why you want to use ArrayList?