-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrowableArray.java
More file actions
53 lines (46 loc) · 1.14 KB
/
Copy pathGrowableArray.java
File metadata and controls
53 lines (46 loc) · 1.14 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
package array;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
public class GrowableArray {
private static Object[] ele= {};
static int size=0;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++) {
if(add(sc.next())) {
System.out.println(Arrays.toString(ele));
}
}
System.out.println(Arrays.toString(ele));
}
public static boolean add(Object element) {
return add(element, size);
}
private static boolean add(Object el,int s) {
if (s == ele.length) {
if(checkDuplicate(el)) {
ele = grow();
ele[s] = el;
// System.out.println(Arrays.toString(ele));
size = s + 1;
return true;
}
}
return false;
}
private static Object[] grow() {
return copy(size+1);
}
private static Object[]copy(int size){
return ele=Arrays.copyOf(ele,size );
}
private static boolean checkDuplicate(Object e) {
for(int i=0;i<ele.length;i++) {
if(e.equals(ele[i])) {
return false;
}
}
return true;
}
}