I created a program that can generate the coordinates of a 2D plane with a 2D array and throw it into an ArrayList at any time.
Currently, I am struggling to create a maze generation algorithm that uses the wall stretching method, but on the way I realized that I had to prepare "something to record the coordinates of the wall under construction". .. (Reference site: Maze generation algorithm (wall stretching method))
Since the wall is generated randomly, the coordinates to be recorded = the number of elements are not constant. Therefore, I vaguely think that it would be nice to have something like the image below.
As a result of various investigations, I found out that the ArrayList that exists in Java is "an array that extends the length without permission". However, it may be a matter of how to check it, but most of them are int type and String type, and I couldn't find anything that handles arrays.
Therefore, I tried to create an ArrayList that can handle two-dimensional arrays, and certain operations have come to be performed, so I will show it below while recording. In addition, I referred to the following site when creating it. (Reference: Multidimensional array in ArrayList)
(1) Creating an ArrayList (2) Generation of a two-dimensional array for substitution (3) Use the add command to add the array generated in (2) to the ArrayList. (4) Use the get command to read the data added in (3).
Generics.java
import java.util.ArrayList;
public class Generics{
public static void main(String[] args){
/*
A program that adds a 2D array to an ArrayList and gets the values
→ When adding, generate an array and array.add
→ array when getting the value.get
*/
ArrayList<Integer[]> array = new ArrayList<Integer[]>();
System.out.println("---------------point1------------------");
Integer[][] point1 = {{6,7}};
array.add(point1[0]);
System.out.printf("(x,y)=(%d,%d)\n",array.get(0)[0],array.get(0)[1]);
System.out.println("---------------point2------------------");
Integer[][] point2 = {{7,4},{2,1}};
array.add(point2[0]);
array.add(point2[1]);
System.out.printf("(x,y)=(%d,%d)\n",array.get(1)[0],array.get(1)[1]);
System.out.printf("(x,y)=(%d,%d)\n",array.get(2)[0],array.get(2)[1]);
System.out.println("---------------point3------------------");
Integer[][] point3 = {{1,2},{4,6},{3,6}};
array.add(point3[0]);
array.add(point3[1]);
array.add(point3[2]);
System.out.printf("(x,y)=(%d,%d)\n",array.get(3)[0],array.get(3)[1]);
System.out.printf("(x,y)=(%d,%d)\n",array.get(4)[0],array.get(4)[1]);
System.out.printf("(x,y)=(%d,%d)\n",array.get(5)[0],array.get(5)[1]);
/*Execution result
---------------point1------------------
(x,y)=(6,7)
---------------point2------------------
(x,y)=(7,4)
(x,y)=(2,1)
---------------point3------------------
(x,y)=(1,2)
(x,y)=(4,6)
(x,y)=(3,6)*/
}
}
As a whole, it's a program that "gets working for the time being, but I'm not sure what's going on and how." Especially in the declaration of ArrayList, I have no idea why the generation and acquisition work well in that way. Perhaps it's because you don't really know what an ArrayList is in the first place, and you're doubly obscured by a stack of generics that you don't really understand.
I also think, "If you want to deal with things that you don't understand so far without knowing them well, why not make an appropriate class and list them?" Maybe there is a more efficient way to write it.
For the time being, in the future, while creating a maze, ・ What is ArrayList? ・ What is Generics? Will be investigated further.
Recommended Posts