I need your help.
for example I input 3 elements
Enter any number= 1
Enter any number= 2
Enter any number= 3
The output will be:public int getNum(){ if(n<=99){ for(int i=0;i<n;i++){ System.out.print("Enter any number: "); ar[i]=br.nextInt(); } } else System.out.print("Out of Bound!!"); return n; }
1 2 3
suppose I want to insert 6 in the position of 2 and the output is
1 6 2 3
and if I delete position 3 the output is
1 6 3
This is a sample code for Insert and Delete.
Can someone help me to explain this following code of insert and delete
public void Insert(){ int j; if(pos<=n) { for( j=n;j>=pos;j--){ ar[j+1]=ar[j];} System.out.print("Insert a new Element : "); ar[j+1] = br.nextInt(); n++; } else System.out.println("Invalid Position"); } public void Delete(){ int j; if(pos<=n) { for(j=pos;j<n;j++){ ar[j]=ar[j+1]; } System.out.println(); System.out.println("Element Deleted"); n--; } else System.out.println("Invalid Position"); }