-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProg6.java
More file actions
121 lines (109 loc) · 3.66 KB
/
Prog6.java
File metadata and controls
121 lines (109 loc) · 3.66 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.util.*;
class Arrays {
ArrayList<String> list = new ArrayList<> ();
ArrayList<String> rev_list = new ArrayList<> ();
public void addItems(String color) {
list.add(color);
}
public void displayItems() {
if(list.size()==0)
System.out.println("Empty list");
else
System.out.println("The items in the list are: " + list);
}
public void toArray() {
if(list.size()==0)
System.out.println("Empty list");
else {
String[] arr = new String[list.size()];
list.toArray(arr);
System.out.println("The array contents are: ");
for (String str : arr)
System.out.println(str);
}
}
public void revList() {
if(list.size()==0)
System.out.println("Empty list");
else {
ArrayList<String> rev = new ArrayList<>();
for (int i = list.size() - 1; i >= 0; i--)
rev.add(list.get(i));
System.out.println("The sorted array is: ");
for (int i = 0; i < rev.size(); i++)
System.out.print(rev.get(i) + " ");
System.out.println("");
}
}
public void getSubarray(int start, int end) {
if(start<0 || end>list.size())
System.out.println("Invalid indices");
else {
ArrayList<String> newlist = new
ArrayList<String>(list.subList(start, end));
System.out.println("Segment of the list from index " +
start + " till index " + end + " is: " + newlist);
}
}
public void sortArray() {
if(list.size()==0)
System.out.println("Empty list");
else {
Collections.sort(list);
System.out.println("The sorted array is: " + list);
}
}
@SuppressWarnings("unchecked")
public void cloneArray() {
if(list.size()==0)
System.out.println("Empty list");
else {
ArrayList<String> clone_list = (ArrayList<String>)
list.clone();
System.out.println("The cloned list is: " + clone_list);
}
}
}
public class Prog6 {
public static void main(String args[]) {
int choice, start, end;
String color;
Scanner s = new Scanner(System.in);
System.out.println("1. Add items to the list\n2. Display items of the list\n3. Convert list to array\n" + "4. Reverse List\n5. Print SubArray\n6. Sort the List\n7. Clone List");
Arrays obj = new Arrays();
while (true) {
System.out.println("Enter the choice: ");
choice = s.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the Color: ");
color = s.next();
obj.addItems(color);
break;
case 2:
obj.displayItems();
break;
case 3:
obj.toArray();
break;
case 4:
obj.revList();
break;
case 5:
System.out.println("Enter the start and end indices (0 based): ");
start = s.nextInt();
end = s.nextInt();
obj.getSubarray(start, end);
break;
case 6:
obj.sortArray();
break;
case 7:
obj.cloneArray();
break;
default:
System.exit(0);
}
}
}
}