Delete element from end in array.
Here we initialize last index with 0(zero) of array.
import java.util.Scanner; class del1{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(“\n Enter the size of array “); intn,g,i,cnt=0,lastIndex=0; n=sc.nextInt(); int array[]=new int[n]; for (i=0; i<n;i++ ) { System.out.println(“enter element : \t”); array[i] = sc.nextInt(); } for (i=0;i<n ; i++) { if (array[i]>0 || array[i]<0) { cnt++; } } System.out.println(“\n before deletion array “); for (i=0;i<n ;i++ ) { System.out.println(“\t”+array[i]); } lastIndex=cnt-1; array[lastIndex]=0; System.out.println(“\n after deletion array “); for (i=0;i<cnt-1 ;i++ ) { System.out.println(“\t”+array[i]); } } } |

Delete element from start in array.
Here we shift every data elements one step towards first index. So last index initialize with 0(zero).
import java.util.Scanner; class del2{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(“\n Enter the size of array “); intn,g,i,cnt=0,lastIndex=0,p=0,q=0,loop=0; n=sc.nextInt(); int array[]=new int[n]; for (i=0; i<n;i++ ) { System.out.println(“enter element : \t”); array[i] = sc.nextInt(); } System.out.println(“\n before deletion array “); for (i=0;i<n ;i++ ) { System.out.println(“\t”+array[i]); } for (i=0;i<n ; i++) { if (array[i]>0 || array[i]<0) { cnt = cnt+1; } } lastIndex=cnt-1; loop = cnt-1; q++; for (i=0;i<loop ;i++ ) { array[p]=array[q]; p++; q++; } array[lastIndex]=0; System.out.println(“\n after deletion array “); for (i=0;i<n ;i++ ) { System.out.println(“\t”+array[i]); } } } |

Delete element from array, from given user’s position.
import java.util.Scanner; class del4{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); intpos,i,n=0,cnt=0; // n = array-size, pos = user array’s delete position System.out.println(“\n enter size of array “); n = sc.nextInt(); int array[] = new int[n]; for (i=0;i<n ;i++ ) { System.out.println(” enter element \t”); array[i]=sc.nextInt(); } System.out.println(“\n enter position to delete “); pos = sc.nextInt(); for (i=0;i<n;i++ ) { if (array[i]<0 || array[i]>0) { cnt=cnt+1; } } System.out.println(” before deletion arry : “); // printing array elements for (i=0;i<n;i++ ) { System.out.println(“\t”+array[i]); } int p=0,q=0; p=pos;q=pos+1; for (i=pos;i<cnt-1;i++ ) { array[p]=array[q]; p++; q++; } p=cnt-1; array[p]=0; System.out.println(” after delete array will look like “); // printing array elements for (i=0;i<n;i++ ) { System.out.println(“\t”+array[i]); } } } |
