求一排序程序

liuchen168 2010-06-22 09:29:05
有几个数字 如“1,23,2,43,67,34”是用逗号分开的,现在要将它们进行降序排列并输出,要求不能使用split()方法?
...全文
40 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuchen168 2010-06-26
  • 打赏
  • 举报
回复
public static void sort()
{
String str = "12,3,423,25,1,53";

//计算 ‘,‘ 号的个数;
int sum=0;
for(char c:str.toCharArray())
{
if(c==',')
{
sum++;
}
}

System.out.println(sum);

String[] aaa = new String[]{};
SortedSet tr=new TreeSet();

for(int i=0;i<sum;i++)
{
String s=str.substring(0, str.indexOf(","));
int b=Integer.parseInt(s);
str=str.substring(str.indexOf(',')+1);
tr.add(b);
}
System.out.println(tr);
}

楼上的兄台很强
closewbq 2010-06-22
  • 打赏
  • 举报
回复

String str="1,23,2,43,67,34";
Pattern p=Pattern.compile(",");
Matcher m=p.matcher(str);
int beginpos=0;
List<String> list=new ArrayList<String>();
while(m.find()){
list.add(str.substring(beginpos, m.end()-1));
beginpos=m.end();
}
Collections.sort(list,new Comparator<String>(){
public int compare(String o1, String o2) {
int a=Integer.parseInt(o1);
int b=Integer.parseInt(o2);
if(a>b)
return -1;
else if(a==b)
return 0;
else
return 1;
}

});
System.out.println(list);
}
}

破题,有split不让用。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧