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