본문으로 건너뛰기

List기본 연습

기본연습

리스트에 요소를 추가하기
public class ExerciseList {

public static void main(String[] args) {

List<String> listExercise = new ArrayList<String>();

listExercise.add("ABC");
listExercise.add("D");
listExercise.add("XYZ");

System.out.println(listExercise);
}
}
[ABC, D, XYZ]

리스트에 요소를 덮어 쓰기
import java.util.ArrayList;
import java.util.List;

public class ExerciseList {

public static void main(String[] args) {

List<String> listExercise = new ArrayList<String>();

listExercise.add("ABC");
listExercise.add("D");
listExercise.add("XYZ");
System.out.println("before : " + listExercise);

// Overwrite index 0
listExercise.set(0, "xxx");

System.out.println("after : " + listExercise);
}
}
before : [ABC, D, XYZ]
after : [xxx, D, XYZ]

두개의 리스트를 병합하기
mport java.util.ArrayList;
import java.util.List;

public class ExerciseList {

public static void main(String[] args) {

List<String> firstList = new ArrayList<String>();

firstList.add("123");
firstList.add("456");
firstList.add("789");

List<String> secondList = new ArrayList<String>();

secondList.add("111");
secondList.add("222");
secondList.add("333");

// firstList secondList Merge

firstList.addAll(secondList);

System.out.println(firstList);


// secondList thirdList Merge Index
List<String> thirdList = new ArrayList<String>();

thirdList.add("Hello");
thirdList.add("World");

thirdList.addAll(1, secondList);

System.out.println(thirdList);
}
}
[123, 456, 789, 111, 222, 333]
[Hello, 111, 222, 333, World]

리스트의 사이즈를 알아보기
import java.util.ArrayList;
import java.util.List;

public class ExerciseList {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();
System.out.println(list.size());

list.add("Hello");
list.add("World");
list.add("!!");
System.out.println(list.size());
}
}
0
3

리스트가 비어있는지 확인
mport java.util.ArrayList;
import java.util.List;

public class ExerciseList {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();
System.out.println(list.isEmpty());

list.add("Hello");
list.add("World");
list.add("!!");
System.out.println(list.isEmpty());
}
}
true
false

리스트의 요소를 꺼내보기
import java.util.ArrayList;
import java.util.List;

public class ExerciseList {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();

list.add("Hello");
list.add("World");
list.add("!!");
System.out.println("Print List Object : " + list);
System.out.println("Print List index 0 : " + list.get(0));
System.out.println("Print List index 1 : " + list.get(1));
System.out.println("Print List index 2 : " + list.get(2));
}
}
Print List Object : [Hello, World, !!]
Print List index 0 : Hello
Print List index 1 : World
Print List index 2 : !!

리스트의 요소를 순차적으로 꺼내기
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExerciseList {

public static void main(String[] args) {

List<String> employeesList = new ArrayList<String>();

employeesList.add("David");
employeesList.add("Bob");
employeesList.add("Cavin");
employeesList.add("Lucy");
employeesList.add("Jackson");

System.out.println("Iterator");
System.out.println("--------");
// Iterator
for(Iterator<String> employee = employeesList.iterator(); employee.hasNext();) {
System.out.println(employee.next());
}

// For
System.out.println("--------");
System.out.println("For");
System.out.println("--------");
for(int i = 0; i < employeesList.size(); i++) {
System.out.println(employeesList.get(i));
}

// Range For
System.out.println("---------");
System.out.println("Range For");
System.out.println("---------");
for(String employee : employeesList) {
System.out.println(employee);
}

// Collection Stream
System.out.println("-----------------");
System.out.println("Collection Stream");
System.out.println("-----------------");
employeesList.forEach((employee) -> {
System.out.println(employee);
});
}
}
Iterator
--------
David
Bob
Cavin
Lucy
Jackson
--------
For
--------
David
Bob
Cavin
Lucy
Jackson
---------
Range For
---------
David
Bob
Cavin
Lucy
Jackson
-----------------
Collection Stream
-----------------
David
Bob
Cavin
Lucy
Jackson

리스트의 요소를 삭제하기
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExerciseList {

public static void main(String[] args) {

List<String> employeesList = new ArrayList<String>();

employeesList.add("David");
employeesList.add("Bob");
employeesList.add("Cavin");
employeesList.add("Lucy");
employeesList.add("Jackson");

System.out.println("List Size : " + employeesList.size());

employeesList.remove(0);
System.out.println(employeesList);

System.out.println("List Size : " + employeesList.size());
}
}
List Size : 5
[Bob, Cavin, Lucy, Jackson]
List Size : 4

리스트의 모든 요소를 삭제하기
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExerciseList {

public static void main(String[] args) {

List<String> employeesList = new ArrayList<String>();

employeesList.add("David");
employeesList.add("Bob");
employeesList.add("Cavin");
employeesList.add("Lucy");
employeesList.add("Jackson");

System.out.println("List Size : " + employeesList.size());
System.out.println(employeesList);

employeesList.clear();
System.out.println("List Size : " + employeesList.size());
System.out.println(employeesList);
}
}
List Size : 5
[David, Bob, Cavin, Lucy, Jackson]
List Size : 0
[]