When solving a Java problem, I was told to "use an array" as a condition for creating a program. "Well, is it Akan in ArrayList?" "What was an array in the first place, I only recognize it very vaguely." I felt that, so I investigated various things. The experience of touching multiple languages has an adverse effect, and all keywords such as "list", "tuple", "array", and "ArrayList" are mixed up. Even if it is limited to Java, there is still a Linked List, and it is quite difficult to organize.
I felt that it was difficult for me to grasp it comprehensively and deeply, and it was also difficult for me to summarize it in an article. I tried to make it feel fluffy and say, "I'm not wrong for the time being." I don't feel like I can understand and explain generics and implementations yet ...
Paiza.io : openjdk version "15" 2020-09-15)
--Arrays and ArrayLists are different like this --"Write this in an array" vs "Write this in an ArrayList" --Declaration --Element assignment --Add element --Getting elements --Getting the number of elements (size)
I researched various things, but anyway, the more I dig, the more information I get, and The content is also rich, so it's easy to keep.
The array is fixed length. The number of elements is fixed when declared and cannot be changed. For example, if you say "Let's set this to contain 5 elements!", You cannot insert the 6th element.
ArrayList, on the other hand, is variable length. For the time being, I set the initial number of elements (or it is set by default even if I do not specify it) Even if the number of elements is exceeded, it is possible to throw in elements one after another.
Supplement
The size can be changed, but it will create an ArrayList that can store 10 elements as the initial size. Don't worry too much about this size as it will automatically grow if you run out of it. Let's programming easy-to-understand site even for beginners
Create an int type array called hoge and put an integer from 1 to 5 in it. When I looked it up for the time being, I found three types of hits, so I will list them.
int[] hoge;
hoge = new int[5];
hoge[0] = 1;
hoge[1] = 2;
hoge[2] = 3;
hoge[3] = 4;
hoge[4] = 5;
int[] hoge = new int {1, 2, 3, 4, 5}
int [] hoge = {1, 2, 3, 4, 5}
Create an Integer type ArrayList called hoge and enter an integer from 1 to 5.
When I looked it up for the time being, I found three types of hits, so I will list them.
Integer
? Isn't it int
? If you think
You may be happy to search around "Java Primitive Wrapper Class".
ArrayList<Integer> hoge = new ArrayList<Integer>();
hoge.add(1);
hoge.add(2);
hoge.add(3);
hoge.add(4);
hoge.add(5);
//As another way of writing
// List<Integer> hoge = new ArrayList<Integer>();
//Or if Java 10 or later
// var hoge = new ArrayList<Integer>();
//You can also write
List<Integer> list = new ArrayList<Integer>(){
{
add(1);
add(2);
add(3);
add(4);
add(5);
};
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
//You can write it like this, but be careful as it will not be variable length.
As an example, assign 5 to the 0th of an array called hoge (ArrayList).
hoge[0] = 5;
hoge.set(0, 5)
Add an int value of 10 as an example
Since it has a fixed length, it cannot be done in the first place.
hoge.add(10);
As an example, take out the 0th element of an array (ArrayList). ** Please note that the types of parentheses are different between Array and ArrayList! ** **
int foo = hoge[0];
int foo hoge.get(0);
int foo = hoge.length
int foo = hoge.size();
I tried to deal with the operations that I thought I would use frequently in the future. I may add it.
--Introduction to Java for a refreshing 3rd edition
Recommended Posts