Progate talks about arrays, but not lists. Arrays and lists are similar, but what's the difference?
python
int result[] = new int[5];
result[0] = 85;
result[1] = 78;
result[2] = 92;
result[3] = 62;
result[4] = 69;
If you compare it to a container, the array result [] must first determine the size of the container. Furthermore, once the size is decided, it cannot be changed after that.
python
List<String> list = new ArrayList<String>();
On the other hand, the size of the list is variable, and elements can be added / deleted.
In java, an interface (like a specification) for handling lists is defined. This List interface is an interface with the roots of Collection.
You need to ʻimrport`` java.util.List` to use List.
python
import java.util.List;
Also, since List is an interface (specification), it has no implementation itself, so It is necessary to ʻimport`` class` which implements List interface separately.
There are ʻArrayList and
LinkedListin
class` that implements List interface.
python
import java.util.list;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
System.out.println(list);
}
}
///Execution result
/// >[]
List<String> list = new ArrayList<>();
It declares that the variable is of type List and that the data handled by this List is of type String.
python
import java.util.list;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
System.out.println(list);
}
}
///Execution result
/// >[]
LinkedList defines some unique methods that List does not have, but they are omitted here. (Almost not in ArrayList)
List cannot directly handle primitive types such as ʻintand
long.
String` is a reference type.
See the table in the previous article for more information on primitive and reference types.
https://qiita.com/hiroshimahiroshi/items/01de02cfe1caacd07540#3-%E5%9F%BA%E6%9C%AC%E3%83%87%E3%83%BC%E3%82%BF%E5%9E%8B%E3%81%A8%E5%8F%82%E7%85%A7%E5%9E%8B
So when dealing with primitive types such as ʻintand
long, the wrapper class ʻInteger
of ʻint, Use the
long wrapper class
Long`.
python
List<Integer> list = new LinkedList<>();
Primitive type? Wrapper class? I'm not sure, but for the time being, I just want to know the list first!
In that case, think that something is strange if the type of List <type>
starts with a lowercase letter for the time being.
Let's learn about primitive types, reference types, and wrapper classes separately.
I edited the article significantly. This article is for people like me who have finished Progate and feel like understanding Java. I've only touched on it, but I think it's important basic knowledge, so let's deepen your understanding with CODEPREP.
Recommended Posts