-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArrayList1.java
109 lines (88 loc) · 2.12 KB
/
ArrayList1.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
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
//------------Define class-----------------------------------------------------------\\
class ArrayList1 implements List1{
Object arr[];
int index;
int size;
int buffer;
ArrayList1(){
size=0;
buffer=5;
arr=new Object[buffer]; //Ambesh
}
//---------------------------Check Index is valid or not--------------------------\\
public void checkindex()
{
Object arr2[]=new Object[buffer*2];
for( int i=0;i<size;i++) {
arr2[i]=arr[i];
}
arr=arr2;
buffer=buffer*2;
}
//-------------------------------------------Add value at last----------------------\\
public void Add(int value) {
if(size==buffer)
{ checkindex();
arr[size]=value;
size++;
}
else {
arr[size]=value;
size++;
}
}
//----------------------Add value at a specific index--------------------------------------\\
public void Add(int value ,int index) {
if(size==buffer)
{ checkindex();
}
try{
if(index<0||index>size) {
BoundException ob = new BoundException();
throw ob;
}
for(int j=size;j>=index;j--) {
arr[j+1]=arr[j];
}
arr[index]=value;
size++;
}
} //--------------------exception handling-----------------------\\
catch(BoundException o) {
o.PrintError();
return;
}
//-------------------------------remove last index------------------------------\\
public void remove(int index) {
try{
if(index<0||index>size) {
BoundException ob = new BoundException();
throw ob;
}
for(int j=size-1;j>=index;j--) {
arr[j]=arr[j+1];
}
size--;
}
catch(BoundException o) {
o.PrintError();
return;
}
//--------------------------------------print all element in list-----------------------\\
public void traverse() {
for(int b=0;b<size;b++) {
System.out.println(arr[b]);
}
}
//-----------------------------------search a element in list--------------------------\\
public void search(int value) {
for(int p=0;p<size;p++){
if(arr[p]==value){
return p;
break;
}
else
System.out.println("element not found");
}
}
}