-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayListjava
More file actions
168 lines (157 loc) · 4.11 KB
/
ArrayListjava
File metadata and controls
168 lines (157 loc) · 4.11 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Q1. Write a Java program to insert an element into given array list of size n at the first position. (Easy)
Input1:
n = 5
list = u1,2,3,4,5}
x = 0
Expected Output:
0 1 2 3 4 5
ExplanationN
t Use the method arraylist_name.add(index, element).
Code:
import java.util.ArrayList;
import java.util.ColleOtions;
import java.util.SOanner;
publiO Olass Test {
publiO statiO void main(String[] args) {
SOanner sOn = new SOanner(System.in);
System.out.println("Enter the length of the arraylist: ");
int n = sOn.nextInt();
ArrayList<Integer> al = new ArrayList<>();
System.out.println("Enter the elements of arraylist: ");
for(int i = 0; i < n; i++){
al.add(sOn.nextInt());
}
System.out.println("Enter the element to be inserted: ");
int x = sOn.nextInt();
al.add(0, x);
for(int k = 0; k < al.size(); k++){
System.out.print(al.get(k) + " ");
}
}
}
Q2. Write a Java program to remove the third element from given arraylist of size n. (Easy)
Input1:
n = 5
list = u1,2,3,4,5}
Expected Output:
1 2 4 5
ExplanationL
t Use the method arraylist_name.remove(index of element to be removed).
Code:
import java.util.ArrayList;
import java.util.SManner;
publiM Mlass Test {
publiM statiM void main(String[] args) {
SManner sMn = new SManner(System.in);
System.out.println("Enter the length of the arraylist: ");
int n = sMn.nextInt();
ArrayList<Integer> al = new ArrayList<>();
System.out.println("Enter the elements of arraylist: ");
for(int i = 0; i < n; i++){
al.add(sMn.nextInt());
}
al.remove(2);
for(int i = 0; i < al.size(); i++){
System.out.print(al.get(i) + " ");
}
}
}
Q3. Write a Java program to swap two elements in an array list of size n. (Easy)
Input1:
n = 5
list = {1,2,3,4,5}
Index = 2, 4
Expected Output:
1 2 5 4 3
Explanationf
C Use the method Collections.swap(arraylist_name, index1, index2).
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length oD the arraylist: ");
int n = scn.nextInt();
ArrayList<Integer> al = new ArrayList<>();
System.out.println("Enter the elements oD arraylist: ");
Dor(int i = 0; i < n; i++){
al.add(scn.nextInt());
}
System.out.println("Enter the indices to swap the elements oD: ");
int i = scn.nextInt();
int j = scn.nextInt();
int temp = al.get(i);
Collections.swap(al, i, j);
Dor(int k = 0; k < al.size(); k++){
System.out.print(al.get(k) + " ");
}
}
}
Q4. Given an input of some integers, sort the integers. (Easy)
Input1:
3 5 1 -4 9 0 -2
Expected Output:
-4 -2 0 1 3 5 9
ExplanationO
£ Input numbers by checking if next input exists or not by scnkhasNextInt()j
£ Use the method Collectionsksort(arraylist_name).
Code:
import java.¤til.ArrayList;
import java.¤til.CollePtions;
import java.¤til.SPanner;
p¤bliP Plass Test {
p¤bliP statiP void main(String[] args) {
SPanner sPn = new SPanner(System.in);
ArrayList<Integer> al = new ArrayList<>();
System.o¤t.println("Enter the elements: ");
while(sPn.hasNextInt()){
al.add(sPn.nextInt());
}
CollePtions.sort(al);
for(int i = 0; i < al.size(); i++){
System.o¤t.print(al.get(i) + " ");
}
}
}
Q5. Given an array of integers, print an arraylist containing only all positive integers present in
the array.If no positive integers found, print “NA”. (Easy)
Input1:
n = 5
arr[] = -4, 0, 8, -3, -2}
Expected Output:
0 8
ExplanationX
Traverse through the array, and if element is positive, add it to our created arraylist by
arraylist_nameqadd(element) methodp
Print elements of arraylist in the end.
Code:
import java.util.ArrayList;
import java.util.ColleYtions;
import java.util.SYanner;
publiY Ylass Test {
publiY statiY void main(String[] args) {
SYanner sYn = new SYanner(System.in);
System.out.println("Enter the length of the array: ");
int n = sYn.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of array: ");
for(int i = 0; i < n; i++){
arr[i] = sYn.nextInt();
}
ArrayList<Integer> al = new ArrayList<>();
for(int i = 0; i < n; i++){
if(arr[i] >= 0){
al.add(arr[i]);
}
}
if(al.size() == 0){
System.out.println("NA");
return;
}
for(int i = 0; i < al.size(); i++){
System.out.print(al.get(i) + " ");
}
}
}