forked from pujakri5438/Hacktoberfest-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicArray.java
More file actions
83 lines (64 loc) · 1.58 KB
/
dynamicArray.java
File metadata and controls
83 lines (64 loc) · 1.58 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
// Java Program to Create a Dynamic Array
// Class 1
// Helper class
class Array {
// Member variables of this class
// Private access modifier
private int arr[];
private int count;
// Note they can only be called through function
// Method 1
// Inside helper class
// to compute length of an array
public Array(int length) { arr = new int[length]; }
// Method 2
// Inside helper class
// To print array
public void printArray()
{
// Iterating over array using for loop
for (int i = 0; i < count; i++) {
// Print the elements of an array
System.out.print(arr[i] + " ");
}
}
// Method 3
// Inside Helper class
public void insert(int element)
{
if (arr.length == count) {
// Creating a new array double the size
// of array declared above
int newArr[] = new int[2 * count];
// Iterating over new array using for loop
for (int i = 0; i < count; i++) {
newArr[i] = arr[i];
}
// Assigning new array to original array
// created above
arr = newArr;
}
arr[count++] = element;
}
}
// Class 2
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of Array(user-defined) class
Array numbers = new Array(3);
// Adding elements more than size specified above
// to the array to illustrate dynamic nature
// using the insert() method
// Custom input elements
numbers.insert(10);
numbers.insert(30);
numbers.insert(40);
numbers.insert(50);
// Calling the printArray() method to print
// new array been dynamically created
numbers.printArray();
}
}