-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tugaspend2_no4.java
75 lines (63 loc) · 2.31 KB
/
Tugaspend2_no4.java
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
71
72
73
74
75
import java.util.*;
public class TugasPend24 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Array
int[] array = new int[5];
System.out.println("Masukkan 5 elemen untuk Array:");
for (int i = 0; i < 5; i++) {
array[i] = scanner.nextInt();
}
System.out.println("Elemen dalam Array: " + Arrays.toString(array));
// ArrayList
ArrayList<Integer> arrayList = new ArrayList<>();
System.out.println("Masukkan elemen untuk ArrayList (selesai dengan -1):");
while (true) {
int element = scanner.nextInt();
if (element == -1)
break;
arrayList.add(element);
}
System.out.println("Elemen dalam ArrayList: " + arrayList);
// Vector
Vector<Integer> vector = new Vector<>();
System.out.println("Masukkan elemen untuk Vector (selesai dengan -1):");
while (true) {
int element = scanner.nextInt();
if (element == -1)
break;
vector.add(element);
}
System.out.println("Elemen dalam Vector: " + vector);
// LinkedList
LinkedList<Integer> linkedList = new LinkedList<>();
System.out.println("Masukkan elemen untuk LinkedList (selesai dengan -1):");
while (true) {
int element = scanner.nextInt();
if (element == -1)
break;
linkedList.add(element);
}
System.out.println("Elemen dalam LinkedList: " + linkedList);
// Stack
Stack<Integer> stack = new Stack<>();
System.out.println("Masukkan elemen untuk Stack (selesai dengan -1):");
while (true) {
int element = scanner.nextInt();
if (element == -1)
break;
stack.push(element);
}
System.out.println("Elemen dalam Stack: " + stack);
// Queue
Queue<Integer> queue = new LinkedList<>();
System.out.println("Masukkan elemen untuk Queue (selesai dengan -1):");
while (true) {
int element = scanner.nextInt();
if (element == -1)
break;
queue.offer(element);
}
System.out.println("Elemen dalam Queue: " + queue);
}
}