forked from Arsh150701/DATA-STRUCTURE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.java
More file actions
91 lines (84 loc) · 1.34 KB
/
ArrayList.java
File metadata and controls
91 lines (84 loc) · 1.34 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
package list;
public class ArrayList<D> implements List<D>
{
Integer buffer;
Integer size;
Object arr[];
public ArrayList()
{
buffer = 5;
size = 0;
arr=new Object[buffer];
}
public void add(D i)
{
arr[size]=i;
size++;
if(size == buffer)
{
Object arr2[]=new Object[buffer*2];
buffer*=2;
for (Integer d=0;d<size;d++)
{
arr2[d]=arr[d];
}
arr=arr2;
}
}
public void traverse()
{
for(Integer j=0;j<size;j++)
{
System.out.print(arr[j]+" ");
}
System.out.println();
}
public void add(D m,Integer pos)
{
for(Integer i=size;i>pos;i--)
arr[i]=arr[i-1];
arr[pos]=m;
size++;
}
public void remove(D m)
{
Integer pos=search(m);
for(Integer i=pos;i<size;i++)
arr[i]=arr[i+1];
size--;
}
public Integer search(D m)
{
for(Integer i=0;i<size;i++)
{
if(arr[i]==m)
return i;
}
return -1;
}
public void checkIndex(Integer pos)
{
Integer size=getSize();
if(pos<0||pos>size)
{
throw new IndexOutOfBoundsException("index = "+pos+"\nsize = "+size+"\n");
}
else System.out.println("index position found");
}
public Integer getSize()
{
return size;
}
public void reverse()
{
Object arr3[]=new Object[buffer];
for(Integer i=0,j=size;i<size;i++,j--)
arr3[i]=arr[j];
arr=arr3;
}
public D get(Integer pos)
{
checkIndex(pos);
return (D)arr[pos];
}
}