Hello, My name is Iwatchi.
About two-dimensional arrays such as Java ArrayList \ <ArrayList \
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.
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.
that's all. There was no site that specifically mentioned this, so I posted it.
I hope you can help me.
Recommended Posts