Sunday 15 June 2014

How delete a number into an array of java? -



How delete a number into an array of java? -

this simple programme array in java. want delete number @ user want not working in code, lastly 2 element proper working @ starting of element deleting not working , don't want utilize of arraylist in code.delete 1 element @ user want nowadays in array.

import java.util.scanner; public class delete { public static void main(string[] args) { // todo auto-generated method stub scanner in=new scanner(system.in); system.out.println("pls come in size of array:-->"); int n=in.nextint(); int num[]=new int[n]; for(int k=0;k<num.length;k++) { system.out.println("enter value:--> "+(k+1)); num[k]=in.nextint(); } system.out.println("enter number delete:-->"); int m=in.nextint(); for(int p=0;p<num.length-1;p++) { if(m==num[p]) { num[p]=num[p+1]; } } for(int k=0;k<num.length-1;k++) system.out.println(num[k]); } }

i recommend utilize arraylist instead of array (check java documentation more informatio), if want way should this:

just add together line

for(int p=0;p<num.length-1;p++) { if(m==num[p]) { num[p]=num[p+1]; // add together line m = num[p]; } }

this because when find number, changing value of next element, instead of remaining elements

one thing should note not deleting element, overwriting values.

java arrays element

No comments:

Post a Comment