티스토리 뷰
ArrayList
The ArrayList class is a resizable array, which can be found in the java.util package.
Add Items
The ArrayList class has many useful methods. To add elements to the ArrayList, use the add() method:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> people = new ArrayList<>();
people.add("Mom");
people.add("Dad");
people.add("Sister");
System.out.println(people);
}
}
Output
[Mom, Dad, Sister]
Access an Item
To access an element in the ArrayList, use the get() method and refer to the index number:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> people = new ArrayList<>();
people.add("Mom");
people.add("Dad");
people.add("Sister");
**System.out.println(people.get(1));**
}
}
Output
Dad
Change and remove an Item
To modify an element, use the set() method and refer to the index number:
To remove an element, use the remove() method and refer to the index number:
public class Main {
public static void main(String[] args) {
ArrayList<String> people = new ArrayList<>();
people.add("Mom");
people.add("Dad");
people.add("Sister");
people.set(2, "Teacher");
System.out.println(people);
people.remove(1);
System.out.println(people);
}
}
Output
[Mom, Dad, Teacher]
[Mom, Teacher]
ArrayList Size
To find out how many elements an ArrayList have, use the size method:
public class Main {
public static void main(String[] args) {
ArrayList<String> people = new ArrayList<>();
people.add("Mom");
people.add("Dad");
people.add("Sister");
people.set(2, "Teacher");
System.out.println(people);
people.remove(1);
System.out.println(people);
System.out.println(people.size());
}
}
Output
2
Sort a given array List
To sort an ArrayList in ascending order, you can use the Collections.sort() method.
public class Main3 {
public static void main(String[] args) {
List<Integer> list_Integer = new ArrayList<Integer>();
list_Integer.add(1);
list_Integer.add(2);
System.out.println(list_Integer);
// Add an element through Scanner
Scanner sc = new Scanner(System.in);
// Enter 5 > 4 > 3
for(int i=1; i<4; i++) {
System.out.println("Enter Number : " );
int a = sc.nextInt();
if(a>=3) {
list_Integer.add(a);
}else {
return;
}
}
System.out.println(" List Before sort: " + list_Integer);
Collections.sort(list_Integer);
System.out.println("List After sort: " + list_Integer);
}
}
Output
[1, 2]
Enter Number :
5
Enter Number :
4
Enter Number :
3
List Before sort: [1, 2, 5, 4, 3]
List After sort: [1, 2, 3, 4, 5]
Sort and reverse elements in a array list
To sort an ArrayList in ascending order, you can use the Collections.sort() method.
To reverse an ArrayList, you can use the Collections.reverse() method.
public class Main3 {
public static void main(String[] args) {
List<Integer> list_Integer = new ArrayList<Integer>();
list_Integer.add(1);
list_Integer.add(2);
System.out.println(list_Integer);
// Add an element through Scanner
Scanner sc = new Scanner(System.in);
// Enter 5 > 4 > 3
for(int i=1; i<4; i++) {
System.out.println("Enter Number : " );
int a = sc.nextInt();
if(a>=3) {
list_Integer.add(a);
}else {
return;
}
}
System.out.println(" List Before sort: " + list_Integer);
Collections.sort(list_Integer);
System.out.println("List After sort: " + list_Integer);
Collections.reverse(list_Integer);
System.out.println("List After reverse: " + list_Integer);
}
}
Output
[1, 2]
Enter Number :
5
Enter Number :
4
Enter Number :
3
List Before sort: [1, 2, 5, 4, 3]
List After sort: [1, 2, 3, 4, 5]
List After reverse: [5, 4, 3, 2, 1]
'Programming Language > JAVA' 카테고리의 다른 글
[Servlet/서블릿] What is a Servlet? / Methods in the Servlet lifecycle / Servlet operation process (0) | 2023.11.10 |
---|---|
[Java] Switch / 자바 스위치 (0) | 2023.10.11 |
[Java] Checking whether the Character is vowel or consonant. / 문자가 모음인지 자음인지 판별하기 (0) | 2023.10.10 |
[Java] Modifier (1) | 2023.10.10 |
[Java] Vector (1) | 2023.10.10 |