学习JAVA遇到问题请大家帮帮忙,是个小问题!谢谢 public static int parseInt(String s) throws NumberFormatException 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的这些数据来自文件,是按文件里的顺序读出来的,你想排序的话,那得把这些数据加入到一个Collection再用Collections.sort();来排序 那怎么把数据加入到Collection中呢?谢谢 int[] arr = new int[st.countTokens()]; int index=0; while (st.hasMoreTokens()){ arr[index++] = Integer.parseInt(st.nextToken()); } 你的文件:t.txt 如下12 56 3 15 8 20 9 1716 19 2 78 95 61 32======================================Sort.java如下:import java.io.*;import java.util.*;public class Sort{ public Sort() throws IOException, FileNotFoundException{ ArrayList list = new ArrayList(); BufferedReader bf = new BufferedReader( new FileReader("t.txt")); String line; while((line = bf.readLine()) != null){ StringTokenizer st = new StringTokenizer(line," "); while(st.hasMoreTokens()){ //添加到ArrayList list.add(st.nextToken()); } } //对ArrayList排序 Collections.sort(list, new Comparator(){ public int compare(Object o1, Object o2){ return Integer.parseInt((String)o1) - Integer.parseInt((String)o2); } }); System.out.println(list); } public static void main(String[] args){ try{ new Sort(); }catch(Exception e){ e.printStackTrace(); } }} List<Integer> li = new ArrayList<Integer>();while((line = in.readLine()) != null){ String[] sNum = line.split("\\s*,\\s*"); for (String str: sNum){ li.add(Integer.parseInt(str)); }}Collections.sort(li);Integer[] arr = new Integer[0];arr = li.toArray(arr); 刺手问题,关于写通用方法~用泛型?反射?不懂 java中怎么使用固定的日期格式 java程序要在MYSQL数据库的表中添加数据 请问一下Eclipse 打包的问题!!数据库没有连接 数据库连接 急! JAVA开发环境的问题 新手上路:一般来与数据库连接都会采用连接池,请问高手:连接池的原理是什么? java小程序为什么用appletviewer命令不能进行? jCreator编译后,如何在运行时加上参数???? 求解哪里错了 请问哪里有比较详细介绍SWT的资料或是书籍阿 讨论 关于集合。。。。。。
int index=0;
while (st.hasMoreTokens()){
arr[index++] = Integer.parseInt(st.nextToken());
}
12 56 3 15 8 20 9 17
16 19 2 78 95 61 32======================================
Sort.java如下:import java.io.*;
import java.util.*;public class Sort{
public Sort() throws IOException,
FileNotFoundException{
ArrayList list = new ArrayList();
BufferedReader bf = new BufferedReader(
new FileReader("t.txt"));
String line;
while((line = bf.readLine()) != null){
StringTokenizer st = new StringTokenizer(line," ");
while(st.hasMoreTokens()){
//添加到ArrayList
list.add(st.nextToken());
}
}
//对ArrayList排序
Collections.sort(list, new Comparator(){
public int compare(Object o1, Object o2){
return Integer.parseInt((String)o1)
- Integer.parseInt((String)o2);
}
});
System.out.println(list);
}
public static void main(String[] args){
try{
new Sort();
}catch(Exception e){
e.printStackTrace();
}
}
}
while((line = in.readLine()) != null){
String[] sNum = line.split("\\s*,\\s*");
for (String str: sNum){
li.add(Integer.parseInt(str));
}
}
Collections.sort(li);
Integer[] arr = new Integer[0];
arr = li.toArray(arr);