Array Dsa
Array Dsa
Scanner;
public class Arraytraversal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size, i, numb, pos;
int[] array = new int[10];
System.out.println("enter size of array");
size = input.nextInt();
System.out.println("Enter elements of array");
for (i = 0; i < size; i++) {
//checking if size of array if not filled or over.
if (size >= array.length || size == array.length) {
System.out.println("YOU CAN NOT INSERT ELEMENTS IN ARRAY");
}
//if size is not exceeding array length than enter elements in array
else {
array[i] = input.nextInt();
}
}
//here enter the element in exiting array you want to enter
System.out.println("enter number you want to enter in array");
numb = input.nextInt();
//here enter the position where you want to enter the element in array
System.out.println("enter position where you want to enter number in
array");
pos = input.nextInt();
//i=size-1 is index of the last element in array
// loop will start from last index and move backward
for (i = size - 1; i >= pos - 1; i--) {
if (pos <= 0 && pos >= size) {
System.out.println("Inavlid Position");
} else {
array[i + 1] = array[i];
//enter the element(numb) at the position[pos-1] here
array[pos-1] = numb;
//increasing the size because a new element has been added to the
array
size++;
}
System.out.println("THE ELEMENTS IN ARRAY ARE :");
for (i = 0; i < size; i++) {
System.out.println(array[i]);
}
}
}
}