-
ArrayListsJava 2022. 2. 6. 16:30
자바의 ArrayList에 대해 정리하였습니다.
< Introduction >
When we work with arrays in Java, we’ve been limited by the fact that once an array is created, it has a fixed size. We can’t add or remove elements.
But with ArrayList, we can create mutable and dynamic lists.ArrayList allow us to:
- Store object references as elements
- Store elements of the same type (just like arrays)
- Access elements by index (just like arrays)
- Add elements
- Remove elements
To use an ArrayList, we need to import them from Java’s util package as well:
import java.util.ArrayList;
< Creating ArrayLists >
< Syntax >
// Declaring: ArrayList<DataType> VariableName; // Initializing: VariableName = new ArrayList<DataType>(); // Declaring and initializing in one line: ArrayList<DataType> VariableName = new ArrayList<DataType>();
- Use angle brackets < > to declare the type of the ArrayList.
: These symbols are used for generics. Generics are a Java construct that allows us to define classes and objects as parameters(data-type) of an ArrayList. - For this reason, we can’t use primitive types in an ArrayList
: alternatively, use <Integer>, <Double>, and <Character> generic, etc.
Example)
import java.util.ArrayList; class ToDos { public static void main(String[] args) { ArrayList<String> toDoList = new ArrayList<String>(); } }
< Adding an Item >
To insert an element using add( ) method.
- add( ) method need two arguments
1. index of the new element (optional)
2. value of the new element
Example)
ArrayList<Car> carShow = new ArrayList<Car>(); carShow.add(ferrari); // carShow now holds [ferrari] carShow.add(thunderbird); // carShow now holds [ferrari, thunderbird] carShow.add(1, corvette); // carShow now holds [ferrari, corvette, thunderbird]
+. It is possible to create an ArrayList that holds values of different types.
ArrayList assortment = new ArrayList<>(); assortment.add("Hello"); // String assortment.add(12); // Integer assortment.add(ferrari); // reference to Car // assortment holds ["Hello", 12, ferrari]
: In the above snippet, assortment is an ArrayList that can store different values because we do not specify its type during initialization.
: In this case, the items stored in this ArrayList will be considered Objects
< ArrayList Size >
To know ArrayList's length using size( ) method.
Example)
ArrayList<String> SangHunsToDos = new ArrayList<String>(); SangHunsToDos.add("Study Java"); SangHunsToDos.add("Study English"); SangHunsToDos.add("Go to Travel!"); SangHunsToDos.size(); // 3
< Accessing an Index >
Use the method get( ) to access an index instead of [ ]
Example)
ArrayList<String> SangHunsToDos = new ArrayList<String>(); SangHunsToDos.add("Study Java"); SangHunsToDos.add("Study English"); SangHunsToDos.add("Go to Travel!"); SangHunsToDos.get(2); // Go to Travel!
< Changing a Value >
Use the set( ) method.
- set( ) method need two arguments
1. index of the element
2. value of the element
Example)
ArrayList<String> SangHunsToDos = new ArrayList<String>(); SangHunsToDos.add("Study Java"); SangHunsToDos.add("Study English"); SangHunsToDos.add("Go to Travel!"); SangHunsToDos.set(0, "Study HTML"); // ["Study HTML", "Study English", "Go to Travel!"]
< Removing an Item >
To remove an element using remove( ) method.
- remove( ) method need one parameter :
1. index : the element's index to remove. OR
2. element : the element's value to remove.
Example)
ArrayList<String> SangHunsToDos = new ArrayList<String>(); SangHunsToDos.add("Study Java"); SangHunsToDos.add("Study English"); SangHunsToDos.add("Go to Travel!"); SangHunsToDos.remove(0); SangHunsToDos.remove("Study English"); // ["Go to Travel!"]
< Getting an Item's Index >
To know the position of a certain element using indexOf( ) method.
Example)
// ["Holmes", "Poirot", "Marple", "Spade", "Fletcher", "Conan", "Ramotswe"]; System.out.println(detectives.indexOf("Fletcher")); // 4
'Java' 카테고리의 다른 글
String Methods (0) 2022.02.07 Loops (0) 2022.02.07 Array (0) 2022.02.05 Conditional Operators (0) 2022.02.05 Conditional (0) 2022.02.05