0% found this document useful (0 votes)
11 views

Array

This document defines an ArrayOperation class that implements various methods for manipulating arrays, including inserting and deleting elements at different positions, printing the array, sorting, and searching. It provides a menu-driven program that allows the user to select operations on an array and test the various methods.

Uploaded by

V Udaykumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Array

This document defines an ArrayOperation class that implements various methods for manipulating arrays, including inserting and deleting elements at different positions, printing the array, sorting, and searching. It provides a menu-driven program that allows the user to select operations on an array and test the various methods.

Uploaded by

V Udaykumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.io.

*;

import java.lang.String;

import java.util.Scanner;

class ArrayOperation

private int Array[];

private int capacity;

private int size;

public ArrayOperation()

capacity=30;

size=1;

Array=new int[capacity];

public void insertAtLast(int ele)

if(size==capacity)

return;

else

Array[size++]=ele;

public void print()

for(int i=1;i<size;i++)

System .out.println(Array[i]);
}

public void addAtMiddle(int ele,int pos)

int i =size;

while(i>=pos)

Array[i]=Array[i-1];

i--;

size++;

Array[pos]=ele;

public void delete(int pos)

int i=size;

while(pos<i)

Array[pos]=Array[pos+1];

pos++;

size=size-1;

public void deleteAtLast()

int del_ele=Array[size-1];

System.out.println("last element in the array is"+del_ele+"deleted");

size--;

public void sort()

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

for(int j=i+1;j<size;j++)

if(Array[i]>Array[j])

int temp=Array[i];

Array[i]=Array[j];

Array[j]=temp;

public void search(int ele)

boolean flag=true;

for(int i=1;i<size;i++)

if(Array[i]==ele)

System.out.println("\n\t search is successfull");

System.out.println("\n \t Element found at \t"+i+"\t location");

flag=flag;

break;

if(flag)

System.out.println("\n\t search is unsuccessfull");

System.out.println("\n\t Element is not there in an array");

class ArrayOperationImplement

{
public static void main(String[] args)

ArrayOperation ao=new ArrayOperation();

int ele,pos;

while(true)

Scanner s=new Scanner(System.in);

System.out.println("***MENU FOR ARRAY OPERATION***");

System.out.println("\n \t 1.Insert Element AtLast \n\t 2.Insert Element At Middle \n\t 3.Delete At
Last\n\t 4. Traversing\n\t 5.Sorting\n\t6.Searching\n\t7.Delete the element at middle");

System.out.println("enter your choice");

int ch=s.nextInt();

switch(ch)

case 1: System.out.println("please enter to be inserting");

ele=s.nextInt();

ao.insertAtLast(ele);

break;

case 2: System.out.println("pls enter the element to be inserting");

ele=s.nextInt();

System.out.println("please enter the position where element is to be inserted");

pos=s.nextInt();

ao.addAtMiddle(ele,pos);

break;

case 3:

ao.deleteAtLast();

break;

case 4: System.out.println("\n\t the elements in the arrays are");

ao.print();

break;
case 5: System.out.println("\n\t Elements in the array are sorted please select traverse to see the
sorted elements");

ao.sort();

break;

case 6: System.out.println("Enter the element to be search");

int ser_ele=s.nextInt();

ao.search(ser_ele);

break;

case 7:System.out.println("pls enter pos to be deleted the array element");

pos=s.nextInt();

ao.delete(pos);

break;

default: System.out.println("\n \t invalid selection");

System.out.println("\n\t Do you want to continue");

char say=s.next().charAt(0);

if(say=='N'||say=='n')

System.out.println("thank you");

break;

You might also like