java中String数组问题

String[] ls = {"A","B","C","V","G","H","K","M","N","S","P","U","V","W","S"};
String [] es = {"B","K","S","U","N"};
参照数组es中的字母顺序对数组ls进行优先排序,对没有在es数组中出现的字母排在数组的最后,不用排序,输出排好序的数组ls

告诉你算法
int num=0;
从es的后面开始,如果当前es的元素在ls中出现,ls中的当前元素和es[num++]交换,循环完es,结果就出来了

public class Test
{
public static void main(String[] args)
{
String stemp;

String[] ls = {"A","B","C","V","G","H","K","M","N","S","P","U","V","W","S"};
String [] es = {"B","K","S","U","N"};
int num=0;

for(int i=es.length-1;i>=0;i--)

for(int j=0;j<ls.length,j++)

{
if(ls[j].equals(es[i]))

{
stemp=ls[num];

ls[num++]=ls[j];

ls[j]=temp;

}

}
}
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答