JAVA按着字典顺序排序 10
求答案,数组〖“red”,“yellow”,“Black”,“Green”〗,进行排序,要求首字母按着字典顺序,不区分大小写,谢谢帮忙了,求答案,感激不尽...
求答案,数组〖“red”,“yellow”,“Black”,“Green”〗,进行排序,要求首字母按着字典顺序,不区分大小写,谢谢帮忙了,求答案,感激不尽
展开
5个回答
展开全部
package com.play;
public class Decimal2Binary {
public static void main(String[] args)
{
String [] strArray = new String[]{"red","yellow","Black","Green"};
String t = null;
System.out.println("排序前");
for(String s : strArray)
System.out.print(s+"\t");
int i,j,k;
for(i=0;i<strArray.length-1; i++)
{
k=i;
for(j=i+1;j<strArray.length;j++)
{
Character c1 = Character.valueOf(strArray[j].charAt(0));
Character c2 = Character.valueOf(strArray[k].charAt(0));
if(c1.compareTo(c2)<0)
k=j;
}
if(i!=k)
{
t=strArray[i];
strArray[i]=strArray[k];
strArray[k]=t;
}
}
System.out.println("\n排序后:");
for(String s : strArray)
System.out.print(s+"\t");
}
}
public class Decimal2Binary {
public static void main(String[] args)
{
String [] strArray = new String[]{"red","yellow","Black","Green"};
String t = null;
System.out.println("排序前");
for(String s : strArray)
System.out.print(s+"\t");
int i,j,k;
for(i=0;i<strArray.length-1; i++)
{
k=i;
for(j=i+1;j<strArray.length;j++)
{
Character c1 = Character.valueOf(strArray[j].charAt(0));
Character c2 = Character.valueOf(strArray[k].charAt(0));
if(c1.compareTo(c2)<0)
k=j;
}
if(i!=k)
{
t=strArray[i];
strArray[i]=strArray[k];
strArray[k]=t;
}
}
System.out.println("\n排序后:");
for(String s : strArray)
System.out.print(s+"\t");
}
}
展开全部
如果同时有 new String[] { "ye", "yel", "yellow" };你的排序是ye,yel,yellow
但是如果new String[] { "yel", "ye", "yellow" };排序就变成 排序后:yel ye yellow
但是如果new String[] { "yel", "ye", "yellow" };排序就变成 排序后:yel ye yellow
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sortStr = new String[]{"red","yellow","Black","Green"};
Arrays.sort(sortStr );
System.out.println(Arrays.asList(sortStr ));
Arrays.sort(sortStr );
System.out.println(Arrays.asList(sortStr ));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询