-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArrayInsert.java
More file actions
75 lines (60 loc) · 2.42 KB
/
DynamicArrayInsert.java
File metadata and controls
75 lines (60 loc) · 2.42 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
import java.util.Scanner;
public class DynamicArrayInsert {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int MAX_CAPACITY = 10;
int[] arr = new int[MAX_CAPACITY];
int currentSize = 0;
// --- 1. Get Initial Array Elements from User ---
System.out.print("Enter the initial number of elements (max " + MAX_CAPACITY + "): ");
currentSize = scanner.nextInt();
if (currentSize > MAX_CAPACITY || currentSize < 0) {
System.out.println("Invalid size. Exiting.");
scanner.close();
return;
}
System.out.println("Enter the " + currentSize + " initial elements one by one:");
for (int i = 0; i < currentSize; i++) {
System.out.print("Element " + (i + 1) + ": ");
arr[i] = scanner.nextInt();
}
System.out.print("\nCurrent Array: [");
printArray(arr, currentSize);
// --- 2. Get Element and Position for Insertion ---
if (currentSize >= arr.length) {
System.out.println("\nArray is full! Cannot insert more elements.");
scanner.close();
return;
}
System.out.print("Enter element to insert: ");
int newElement = scanner.nextInt();
System.out.print("Enter position (1 to " + (currentSize + 1) + "): ");
int userPos = scanner.nextInt();
int pos = userPos - 1; // Convert 1-based position to 0-based index
if (pos < 0 || pos > currentSize) {
System.out.println("Invalid position! Insertion failed.");
scanner.close();
return;
}
// --- 3. Insertion Logic (Shifting) ---
// Shift elements to the right to make space
for (int i = currentSize - 1; i >= pos; i--) {
arr[i + 1] = arr[i];
}
// --- 4. Insert Element and Update Size ---
arr[pos] = newElement;
currentSize++;
// --- 5. Display Result ---
System.out.print("\nArray After Insertion: [");
printArray(arr, currentSize);
System.out.println("\nNew Size: " + currentSize);
scanner.close();
}
// Helper method to print array elements
public static void printArray(int[] arr, int size) {
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + (i < size - 1 ? ", " : ""));
}
System.out.println("]");
}
}