| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
- 파이썬
- 배열
- predictive analysis
- 오블완
- Pre-processing
- pointer
- baekjoon
- 알고리즘
- vscode
- raw data
- 함수
- 티스토리챌린지
- 백준
- Class
- string
- 포인터
- const
- OOP
- Deep Learning
- array
- function
- 문자열
- Python
- C++
- assignment operator
- Data Science
- programming
- 반복문
- Object Oriented Programming
- pass by reference
- Today
- Total
Channi Studies
[Java] Generic Types of List 본문
To create an ArrayList (or LinkedList) in Java, you can do as following:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List myList = new ArrayList();
myList.add(10);
myList.add("Go");
myList.add(false);
System.out.println(myList); // [10, Go, false]
}
}
If we do not specify the type of the elements in the List, we can put different types in the List like we did in Python.
Since we can put whatever we want, possible runtime errors can occur dealing with types.
However, there are multiple reasons why we use generics specified within angle brackets (or diamond operator, <>)
One problem that might arise in such situation is incompatible type error when assigning an element to
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List myList = new ArrayList();
myList.add(10);
myList.add("Go");
myList.add(false);
int firstNum = myList.get(0); // Compile Error
// java: incompatible types: java.lang.Object cannot be converted to int
}
}
This error occurs same for "Go" and false. Why does this happen?
It happens because we didn't specified generic type of the List, resulting EVERY elements getting returned to have Object (java.util.Object) type, which is the superclass of every class in Java.
In other words, ANY classes that you define automatically inherits from java.util.Object class.
Since the generic type hasn't provided, Java returns every object in Object type.
Although specifying type is safer, we can still use the code if we add type casting.
Object type will be converted to our desired types with appropriate type casting.
For example:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List myList = new ArrayList();
myList.add(10);
myList.add("Go");
myList.add(false);
int firstNum = (Integer) myList.get(0);
String secondStr = (String) myList.get(1);
boolean thirdBool = (boolean) myList.get(2);
System.out.println(firstNum + ", " + secondStr + ", " + thirdBool);
}
}
Now, there is another weird problem when you try to make a List of integers. (with generics)

My compiler shows "Type argument cannot be of a primitive type" on integer.
Like other programming languages, Java have some primitive types–byte, short, int, long, float, double, boolean, and char.
Those primitive types are NOT objects, while generic system requires objects. So you must only put objects inside the diamond operator (<>).
Then, what should we do in such situation? There are something called wrapper class corresponding to each data type.
Ex) int → Integer, double → Double, boolean → Boolean, etc
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<>();
List<Double> secondList = new LinkedList<>();
myList.add(1);
myList.add(2);
myList.add(3);
System.out.println(myList);
secondList.add(2.0);
secondList.add(2.5);
secondList.add(-3.141592);
System.out.println(secondList);
}
}

'Data Science > Data Structure & Algorithm' 카테고리의 다른 글
| [Java] Stack with IntNode, two-stack reverse trick (0) | 2025.09.08 |
|---|---|
| [Data Structure] Graph: Topological Sort (0) | 2025.05.28 |
| [Python] Bellman-Ford's Shortest Path (0) | 2025.05.28 |
| [Python] Dijkstra's Shortest Path | 다익스트라 최단거리 (0) | 2025.05.27 |
| [Data Structure] Python: Graphs (0) | 2025.05.27 |