插入法(英文歌曲名):
import java.util.*;
public class Demo5{
public static void main(String[] args)
{
String[] musics = new String[]{“Island”,”Ocean”,”Pretty”,”Sun”};
Scanner input = new Scanner(System.in);
musics = Arrays.copyOf(musics,musics.length+1);
System.out.print("输入一首歌曲的名称:");
String musicName = input.next();
int index = musics.length-1;
for(int i = 0;i < musics.length - 1;i++)
{
if(musicName.compareToIgnoreCase(musics[i]) < 0)
{
index = i;
break;
}
}
//System.out.println("index = " + index);
for(int i = musics.length-2;i>=index;i--)
{
musics[i+1] = musics[i];
}
musics[index] = musicName;
for(int i = 0;i < musics.length;i++)
{
System.out.println(musics[i]);
}
}
}