-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayImplementation.java
104 lines (86 loc) · 2.38 KB
/
ArrayImplementation.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
package datastructure;
/*------------------------------------Dynamic Array List Implementation-----------------------------------------by_Abd----*/
// Aarambikalaamaa
import java.util.Arrays;
class ArrDS {
final static int initialCapacity = 2;
private int size;
private int arr[];
private int capacity;
ArrDS() {
arr = new int[initialCapacity];
capacity = initialCapacity;
size = 0;
}
// ----------------Insert Element Into the Array---------------- //
void insertValue(int val)
{
if(size== capacity)
{
doubleTheArray();
}
arr[size]= val;
size++; // when insert an element we must increase the size //
}
private void doubleTheArray()
{
capacity=capacity*2;
arr=Arrays.copyOf(arr, capacity);
}
//-----------------Display Array Element ------------------------//
void displayArrElement()
{
System.out.println("\nElements into the Array is : ");
for(int i=0; i<size; i++)
{
System.out.print(" "+arr[i]+" ");
}
}
//-----------------Insert Value at Particular Position -------------------------//
void insertAtPos(int pos,int val)
{
if(pos >size)
{
System.out.println("\n\nInvalid Position Pls enter valid Position ");
return;
}
for(int i=size-1; i>=pos; i--) //Note this Line very carefully bcz it's decrement//
{
arr[i+1]=arr[i];
}
arr[pos]=val;
size++; // when insert an element we must increase the size //
}
//--------------------------------Deleting Array element------------------------//
void deleteArrElement(int pos)
{
if(pos >size)
{
System.out.println("\n\nInvalid Position Pls enter valid Position ");
return;
}
System.out.println("Deleted Element is : "+arr[pos]);
for(int i=pos; i<size; i++)
{
arr[i]=arr[i+1];
}
size--; // when delete an element we must decrease the size //
}
}
public class ArrayImplementation {
public static void main(String[] args) {
ArrDS list = new ArrDS();
list.insertValue(1);
list.insertValue(2);
list.insertValue(3);
list.insertValue(4);
list.insertValue(5);
list.insertValue(6);
list.displayArrElement();
list.insertAtPos(4, 66);
list.displayArrElement();
list.deleteArrElement(78);
}
}
// Mudichaachuuu!!!
/*---------------------------------------------------------------------------------------------------------------*/