package 上机练习;
import java.util.Scanner;
public class A02 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
String [] names= new String[]{"Computer","Hibernate","Java","Struts"};
String [] newnames= new String[names.length+1];
String name ="";
int index = names.length;
System.out.print("插入前的数组为: ");
for(int i =0;i<names.length;i++){
System.out.print(names[i]+" ");
}
for(int i =0;i<names.length;i++){
newnames[i]=names[i];
}
System.out.print("\n请输入新书的名称: ");
name = input.next();
for(int i =0;i<names.length;i++){
if(names[i].compareToIgnoreCase(name)>0){
index = i;
break;
}
}
for(int i=newnames.length-1;i>index;i--){
newnames[i]=newnames[i-1];
}
newnames[index]=name;
System.out.print("插入后的数组为:");
for(int i=0;i<newnames.length;i++){
System.out.print(newnames[i]+" ");
}
}
}
转载于:https://blog.51cto.com/765133133/1423200