Insertion and delete

 [19/05, 12:15 pm] KIRAN: import java.util.Scanner;


class DeleteFromArray {

    public static void main(String[] args) {

        Scanner r = new Scanner(System.in);

        int size, i, loc;


        System.out.print("Enter size of the array: ");

        size = r.nextInt();

        int a[] = new int[size];


        System.out.print("Enter Array Elements: ");

        for (i = 0; i < size; i++) {

            a[i] = r.nextInt();

        }


        System.out.print("Enter Array location: ");

        loc = r.nextInt();


        for (i = loc; i < size - 1; i++) {

            a[i] = a[i + 1];

        }


        size--;


        System.out.print("Array after deletion: ");

        for (i = 0; i < size; i++) {

            System.out.print(a[i] + " ");

        }

    }

}

[19/05, 12:17 pm] KIRAN: import java.util.Scanner;


class InsertIntoArray {

    public static void main(String[] args) {

        Scanner r = new Scanner(System.in);

        int size, i, loc, item;


        System.out.print("Enter Array Size: ");

        size = r.nextInt();


        int a[] = new int[size + 1]; // Allocate one extra space for insertion


        System.out.print("Enter Array Elements: ");

        for (i = 0; i < size; i++) {

            a[i] = r.nextInt();

        }


        System.out.print("Enter Array location: ");

        loc = r.nextInt();


        System.out.print("Enter new item: ");

        item = r.nextInt();


        for (i = size; i > loc; i--) {

            a[i] = a[i - 1];

        }


        a[loc] = item;

        size++;


        System.out.print("Array after insertion: ");

        for (i = 0; i < size; i++) {

            System.out.print(a[i] + " ");

        }

    }

}

Comments

Popular posts from this blog

class 8 computer chapter 5

CLASS 8 COMPUTER

Class 5 computer chapter 4