Wednesday, June 1, 2011
Menu driven program on dynamic insertion to or deletion from an array
In this array program we will take values in an array first and then take choice from the user which operation he / she wants, to insert a new value at any specific location or wants to delete value from a specified location and required operation will be carried out.
Codes of the array program
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr=new int;
public void take() throws Exception
System.out.println("After inserting the values, array is as follows\n");
System.out.println("\nWhich operation you want, press 1 for insertion and press 2 for deletion:");
void insert() throws Exception
System.out.println("Enter the value to insert:");
System.out.println("Enter the location where to insert:");
for(i=10;i > loc;i--)
System.out.println("After inserting new value, array is as follows\n");
void delete() throws Exception
System.out.println("Enter the location where the value is to be deleted:");
System.out.println("After deleting any value, array is as follows\n");
void display(int s)
public static void main(String args) throws Exception
Array ob=new Array();
Technical analysis of the array program
I think storing values in the array and displaying the array needs no explanations. Size of the array is kept 1 more than the required size as it may have to accommodate one extra value in the existing array. According to user choice, either insert () function or delete () function is invoked. Both the functions are defined in the program. If the job is to insert a new value in the array, then the value and the locations are stored in two separate variables. If the location specified is 5, then the actual location in the array is 4 and that’s why the value of ‘loc’ is reduced by 1 in both the cases, insertion or deletion. In case of inserting new value at any specific location of the array, the values in the array from extreme right to the specific location are copied to it’s right location to make room for the new value. If the case is of deletion from array, the values from the just right location value where the value of the array is to be deleted to the extreme right value are copied to it’s location to eliminate the specific location value of the array.
After each operation, values in the array are displayed with appropriate size value of the array.