Delete a custom array (pseudo-delete)

Source: Internet
Author: User
Tags array length

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.