Inserting an element in an array

Source: Internet
Author: User

1 /*5. Array of songs as follows2 string[] musics = new string[]{"Island", "Ocean", "Pretty", "Sun"};3 //This is an alphabetical sequence of string arrays4 5 now I'm going to insert a new English song Inside,6 insert to the specified position by first alphabetical order7 8 Tip: Musics[i].comparetoignorecase (music) > 09 The above method is a method of comparing strings,Ten if the previous value returns 1 large, if the back is greater than the preceding, returns-1, or equals the preceding, returns 0 One  A For example, "Haha". Comparetoignorecase ("aha") is a comparison of Haha strings and Aha strings (this method automatically ignores case) - h is larger than a, so the return is 1, (according to the alphabet order)*/ -  the  ImportJava.util.*; -  classfive{ -       Public Static voidMain (string[] args) { -Scanner sc =NewScanner (system.in); +string[] Musics =Newstring[]{"Island", "Ocean", "Pretty", "Sun"}; -         intIncre = 1;//Capacity Increase Amount +String[] A =NewString[musics.length+incre];//new Array ASystem.arraycopy (musics, 0, a, 0, musics.length);//copy musics array contents to new array a atSYSTEM.OUT.PRINTLN ("Please insert a new English song"); -String name =sc.nextline (); -A[A.LENGTH-1] =name; -  -Musics = A;//Changing references -  in         intCount = 0; -          for(inti = 0; i<musics.length-1; i++ ) { to              if(Musics[i].comparetoignorecase (name) >0) { +count++; -}Else { theMUSICS[MUSICS.LENGTH-1] =name; *              } $          }Panax Notoginseng          for(inti = musics.length-1;i> = 0; i-- ) { -              if(i>musics.length-count-1) { theMusics[i] = musics[i-1]; +              } A          } themusics[musics.length-count-1]=name; +System.out.println ("Add success:"); -           for(String i:musics) $ System.out.println (i);         -      } -}

Inserting an element in an array

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.