[Java] [For beginners] How to insert elements directly in a 2D array

Introduction

Hello, My name is Iwatchi. About two-dimensional arrays such as Java ArrayList \ <ArrayList \ >.

Background to the posting

The other day, I participated in AtCoder's Beginner Contest for the first time, and at that time I had a problem using a two-dimensional array.

Then, in the model answer, there was a scene where an element was directly inserted into a two-dimensional array. Basically, C ++ is the main code for the model answer, so if you write it in Java, there are some differences from the model answer.

Note that in Java, inserting directly into a two-dimensional array will result in an error. If you think about it carefully, you can do it right away, but I also posted it as a memorandum of my own.

Insert elements directly into a 2D array

In detail, it is not direct w. The way to do it is to define it with new before inserting the element. The method of inserting directly is as follows.

Main.java


ArrayList<ArrayList<Integer>> arrays = new ArrayList<ArrayList<Integer>>();

/*here*/
for (int i = 0; i < index; i++) {
		ArrayList<Integer> array = new ArrayList<Integer>();
		arrays.add(array);
	}
/* */

arrays.get(index - 1).add(2000);

If you do not include this for statement, an error will occur. It seems that you can go straight without defining it as C ++, but it seems that Java is useless.

in conclusion

that's all. There was no site that specifically mentioned this, so I posted it.

I hope you can help me.

Recommended Posts

[Java] [For beginners] How to insert elements directly in a 2D array
How to convert a file to a byte array in Java
How to make a Java array
[For Ruby beginners] Explain how to freely delete array elements!
For Java beginners: List, Map, Iterator / Array ... How to convert?
How to insert a video in Rails
[For beginners] How to debug in Eclipse
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
[Java] How to turn a two-dimensional array with an extended for statement
How to create pagination for a "kaminari" array
[Java] How to search for a value in an array (or list) with the contains method
How to ZIP a JAVA CSV file and manage it in a Byte array
How to create a Java environment in just 3 seconds
How to use an array for a TreeMap key
[For beginners] How to operate Stream API after Java 8
[Spring Boot] How to create a project (for beginners)
How to create a data URI (base64) in Java
[For beginners] Minimum sample to display RecyclerView in Java
How to convert A to a and a to A using AND and OR in Java
How to initialize Java array
How to insert a video
(Java) How to implement equals () for a class with value elements added by inheritance
How to create a lightweight container image for Java apps
Convert a Java byte array to a string in hexadecimal notation
How to change a string in an array to a number in Ruby
How to store a string from ArrayList to String in Java (Personal)
How to output array values without using a for statement
How to add the same Indexes in a nested array
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
How to make a Java container
How to learn JAVA in 7 days
[Java] How to create a folder
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
How to implement a job that uses Java API in JobScheduler
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to automatically operate a screen created in Java on Windows
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
Cast an array of Strings to a List of Integers in Java
How to check for the contents of a java fixed-length string
How to make a groundbreaking diamond using Java for statement wwww
How to make a Java calendar Summary
When seeking multiple in a Java array
How to implement Kalman filter in Java
How to add a new hash / array
[Introduction to Java] How to write a Java program
How to make a Discord bot (Java)
[Ruby] How to use slice for beginners
How to embed Janus Graph in Java
How to print a Java Word document
How to get the date in java
How to publish a library in jCenter
How to assemble JSON directly in Jackson
[Personal memo] How to interact with a random number generator in Java
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to pass a proxy when throwing REST over SSL in Java
How to get the absolute path of a directory running in Java