-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQ_12ArrayList.java
More file actions
57 lines (51 loc) · 2.08 KB
/
Q_12ArrayList.java
File metadata and controls
57 lines (51 loc) · 2.08 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
import java.util.ArrayList;
import java.util.Scanner;
public class Q_12ArrayList{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
ArrayList<String> Q = new ArrayList<String>();
while(true){
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Display");
System.out.println("4. Print elements with length less than 5");
System.out.println("5. Exit!");
System.out.println("Enter your choice:\t");
int ch = sc.nextInt();
switch(ch){
case 1:
System.out.println("Enter the string you wanna enqueue:\t");
String x = sc.next();
Q.add(Q.size(),x);
break;
case 2:
System.out.println("The dequeued string is "+ Q.remove(0));
break;
case 3:
if(Q.isEmpty()){
System.out.println("No elements in the Queue");
}
System.out.println("The elements of the Queue are:");
for(int i=0;i<Q.size();i++){
System.out.println(Q.get(i));
}
break;
case 4:
if(Q.isEmpty()){
System.out.println("No elements in the Queue");
}
System.out.println("The elements with length less than or equal to 5 are:\t");
for(int i=0;i<Q.size();i++){
if(Q.get(i).length() <=5){
System.out.println(Q.get(i));
}
}
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice!");
}
}
}
}