-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList
More file actions
85 lines (77 loc) · 1.89 KB
/
Copy pathArrayList
File metadata and controls
85 lines (77 loc) · 1.89 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
package collectionImpl;
import java.util.ArrayList;
import java.util.Arrays;
public class MyArrayList
{
private Object[] MyArray;
private int actSize=0;
public MyArrayList(){
MyArray = new Object[10];
}
public Object get(int index)
{
if(index<actSize)
return MyArray[index];
else
throw new ArrayIndexOutOfBoundsException();
}
public void add(Object ob)
{
if(MyArray.length-actSize<=5)
{
increaseListSize();
}
MyArray[actSize++]=ob;
}
public Object remove(int index)
{
if(index<actSize)
{
Object ob=MyArray[index];
MyArray[index]=null;
int temp=index;
while(index<actSize)
{
MyArray[temp]=MyArray[temp+1];
MyArray[temp+1]=null;
temp++;
}
actSize--;
return ob;
}
else
throw new ArrayIndexOutOfBoundsException();
}
private void increaseListSize()
{
MyArray=Arrays.copyOf(MyArray,MyArray.length * 2);
System.out.println("New ArrayList lenghth:"+MyArray.length);
}
public int Size()
{
return actSize;
}
public static void main(String[] args)
{
ArrayList<Integer> mal=new ArrayList<Integer>();
mal.add(4);
mal.add(6);
mal.add(8);
mal.add(10);
mal.add(12);
mal.add(14);
for(int i=0;i<mal.size();i++)
{
System.out.println(mal.get(i)+" ");
}
mal.add(16);
System.out.println("Element at index 4:"+mal.get(4));
System.out.println("List size:"+mal.size());
System.out.println("Removing element at index 3: "+mal.remove(3));
System.out.println("DIsplaying the final List:");
for(int i=0;i<mal.size();i++)
{
System.out.println(mal.get(i)+" ");
}
}
}