Scanner input = new Scanner(System.in);
String[] x = new String[] { "Island", "Ocean", "Pretty", "0" };
String[] r = new String[x.length + 1];
System.out.println("插入前的数组为:");
for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + "\t");
r[i] = x[i];
}
System.out.println("\n请输入插入歌曲:");
String name = input.next();
int p = r.length;
for (int k = 0; k < r.length; k++) {
if (r[k].compareToIgnoreCase(name) > 0) {
p = k;
break;
} else {
p = r.length - 1;
}
}
for (int i = r.length - 1; i > p; i--) {
r[i] = r[i - 1];
}
r[p] = name;
System.out.println("\n插入后的数组为:");
for (int i = 0; i < r.length; i++) {
System.out.print(r[i] + "\t");
}
java练习--插入歌曲
最新推荐文章于 2021-01-13 07:33:42 发布