求大神帮忙!编写程序对该数组排序,并输出所有包含“王”字的字符串
但是我的运行运行结果中排序部分的结果不对,是:张合,小王,天王盖地虎,小赵,王敏但是正确的不是应该是张合,小赵,小王,王敏,天王盖地虎 吗?
class string{
String [] name;
public string(String [] name){
this.name = name;
}
void contain_wang(String [] name){
System.out.println("输出带王的字符串:");
for(int i=0;i<name.length;i++){
//对字符进行分割比较
for(int j=0;j<name[i].length();j++){
if(name[i].charAt(j)=='王'){
System.out.println(name[i]+' ');
}
}
}
// System.out.println();
}
void paixu(String [] name){
String name1;
System.out.println("字符串数组排序后的结果是");
//进行冒泡排序
for(int i=0;i<name.length;i++){
for(int j=i;j<name.length-1;j++){
if(name[j].compareTo(name[j+1])>=0){
name1 = name[j];
name[j] = name[j+1];
name[j+1] = name1;
}
}
}
}
void output(String [] name){
for(int i=0;i<name.length;i++){
System.out.println(name[i]+' ');
}
// System.out.println(' ');
}
public static void main(String [] agrs){
String [] name1 = {"王敏","张合","小王","小赵","天王盖地虎"};
string name = new string(name1);
name.contain_wang(name1);
name.paixu(name1);
name.output(name1);
}
}