Delete the first to find the index to delete the element, and then assign the value after the index to the previous one, the last array length minus one
int[] arr = {1,3,5,7,8,15,22,33,44,55,66}; Scanner SC=NewScanner (system.in);//user keyboard input to find the numberSystem.out.println ("Please enter the number you want to find"); intfindnum=Sc.nextint (); intlow=0;//Set minimum Value intHeight=arr.length-1;//Set maximum value intMid=0;//index of the middle value to be obtained intCount=arr.length;//gets the total length of the array BooleanIsfind=false;//flag indicates whether to find intDelindex=-1;//set the index of the element to delete while(Height>=low) {//because you do not know how many times the loop is used while loop, and the maximum value is always greater than or equal to the minimum value if less than the repeated lookupMid= (low+height)/2; if(findnum>Arr[mid]) { Low=mid+1;//when the minimum value is greater than the median value, the index changes to an intermediate value of +1. } Else if(findnum<Arr[mid]) {Height=mid-1;//change to the maximum value when the size is in the middle value index is the median -1 } Else if(Findnum==arr[mid]) {//jumps out of the loop when the condition is met and assigns the flag bit to TrueIsfind=true; System.out.println ("Congratulations found the index for" +mid); Delindex=mid;//gets the index of the number to be deleted Break; } } if(isfind!=true) {System.out.println ("The number you have entered cannot be found"); } Count--;//after deletion, the array length is +1 for(inti = mid; I <count; i++) {Arr[i]=arr[i+1]; } for(inti = 0; I < count; i++) {System.out.println (arr[i]); }
Delete a custom array (pseudo-delete)