[JAVA] Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane

Overview

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.

Details

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.

伸縮自在な座標入れ.png

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)

procedure

(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)*/
	}
}

Digression

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

Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
A program (Java) that outputs the sum of odd and even numbers in an array
A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
Want to throw an IOException out of Stream? in that case
The story of forgetting to close a file in Java and failing
[Android] Develop a service that allows university students to check the operating status of buses circulating in the university.
A simple application (CT) that allows you to view the RDSR in which the radiation exposure dose is recorded.
A solution to an error that makes you angry that you are not following the MySQL default setting ONLY_FULL_GROUP_BY in a production environment and it is not unique.
I tried to make an app that allows you to post and chat by genre ~ Where I had a hard time ~
Pursuing the mystery that the number of DB connections in Tomcat increases to only 8-A day of an OSS support engineer
What to do if you can't get the text of an element in Selenium
A method that applies transforms in parallel to all elements and returns an array of return values that maintain their order
I tried to make an app that allows you to post and chat by genre ~ App overview ~
A story that I realized that I had to study as an engineer in the first place
Determine that the value is a multiple of 〇 in Ruby
How to retrieve the hash value in an array in Ruby
[Ruby] How to retrieve the contents of a double hash
A program that counts the number of words in a List
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
[Swift] When you want to know if the number of characters in a String matches a certain number ...
A collection of patterns that you want to be aware of so as not to complicate the code
Throw an exception and catch when there is no handler corresponding to the path in spring
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
Try to imitate the idea of a two-dimensional array with a one-dimensional array
[Swift5] How to get an array and the complement of arrays
Cast an array of Strings to a List of Integers in Java
How to get the length of an audio file in java
When you receive a call, send an SMS to that number
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
Are you still exhausted to implement the search function? Gem'ransack' that can be implemented in an instant
Ubuntu 20.04 The story of creating CFn that installs CloudWatch agent on LTS and creates a configuration file
How to set an image in the drawable Left / Right of a button using an icon font (Iconics)
The story of introducing Gradle as a retrofit to an existing system that did not manage packages
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
An error occurred in the free course of RubyOnRails Udemy, solved it, and went through to the end
This and that of the JDK
A memo that was soberly addicted to the request of multipart / form-data
A memo when you want to clear the time part of the calendar
How to test a private method in Java and partially mock that method
Link Apache and Tomcat in a blink of an eye on CentOS 8
A collection of phrases that impresses the "different feeling" of Java and JavaScript
To you who lament that the conversion of JODConverter + LibreOffice is slow
A program that determines whether the entered integer is close to an integer
Confirmation and refactoring of the flow from request to controller in [httpclient]
This and that of the implementation of date judgment within the period in Java
How to change the maximum and maximum number of POST data in Spark
Sample program that returns the hash value of a file in Java
We have released a service that allows you to easily create chats!
How to change the value of a variable at a breakpoint in intelliJ
How to get the absolute path of a directory running in Java
Find the maximum and minimum of the five numbers you entered in Java
A story about an arithmetic overflow that you shouldn't encounter in Ruby
Ubuntu 18 is the OS that adds a NIC to a server instance in Sakura's cloud and assigns a local IP.
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
I tried to make a web application that searches tweets with vue-word cloud and examines the tendency of what is written in the associated profile