Repository navigation
Expand file tree
/
Copy pathArrayListTest.java
More file actions
70 lines (64 loc) · 2.01 KB
/
Copy pathArrayListTest.java
File metadata and controls
70 lines (64 loc) · 2.01 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package test;
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListTest {
public static void main(String[] args) {
System.out.println("lab5_1: 용동중");
Scanner input = new Scanner(System.in);
// (1) Integer를 타입 매개변수로 하여 ArrayList 객체를 생성(list라는 이름의 변수에 저장)
ArrayList<Integer> list = new ArrayList<Integer>();
// 완성하세요.
// (2) 사용자에게 메뉴를 반복 제공하고 7을 입력하면 반복 종료
int menu = 0;
int index = 0;
int element = 0;
do {
System.out.print(" 1:인덱스조회 2:전체조회 3:길이조회 4:삽입 5:인덱스삽입 6:인덱스삭제 7:종료 ---> ");
menu = input.nextInt();
switch(menu) {
case 1:
System.out.println("인덱스 위치의 원소값을 조회합니다.");
System.out.print("인덱스 입력: ");
index = input.nextInt();
element = list.get(index); // 완성하세요.
System.out.println(element);
break;
case 2:
System.out.println("리스트 전체를 조회합니다.");
System.out.println(list.toString()); // 완성하세요.
break;
case 3:
System.out.println("리스트 길이를 조회합니다.");
int size = list.size(); // 완성하세요.
System.out.println(size);
break;
case 4:
System.out.println("원소를 리스트의 맨 뒤에 삽입합니다.");
System.out.print("정수값 입력: ");
element = input.nextInt();
list.add(element);; // 완성하세요.
break;
case 5:
System.out.println("인덱스 위치에 원소를 삽입합니다.");
System.out.print("인덱스 입력: ");
index = input.nextInt();
System.out.print("정수값 입력: ");
element = input.nextInt();
list.add(index, element); // 완성하세요.
break;
case 6:
System.out.println("인덱스 위치의 원소를 삭제합니다.");
System.out.print("인덱스 입력: ");
index = input.nextInt();
element = list.remove(index); // 완성하세요.
System.out.println(element);
break;
case 7:
System.out.println("종료합니다.");
break;
default:
System.out.println("메뉴 선택 오류: 다시 선택하세요.");
}
} while(menu != 7);
}
}