티스토리 뷰

728x90

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]
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
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
글 보관함