初学java,如何写一个自己的source reader来读自己的数据,我的数据集和格式如下:
1,23,4,0
1,830,42,0
1,330,319,0
1,459,46,0
1,49,571,0
2,22,4,0
2,29,448,0
2,29,278311,0
2,29,432,0
2,117,48,0
3,490,359,0
4,83,67,0
4,82,22,0
5,67,48,0
6,228,2,0
6,228,19,0
6,32,12,0
6,12,12,0
7,57,167,0
。
。
1,23,4,0
1,830,42,0
1,330,319,0
1,459,46,0
1,49,571,0
2,22,4,0
2,29,448,0
2,29,278311,0
2,29,432,0
2,117,48,0
3,490,359,0
4,83,67,0
4,82,22,0
5,67,48,0
6,228,2,0
6,228,19,0
6,32,12,0
6,12,12,0
7,57,167,0
。
。
解决方案 »
- 今天做的赶集网的笔试题,大家有兴趣讨论一下
- ServerSocket读了再写的问题
- 急!如何编写一个Java的GUI 压缩程序???
- 请问有谁懂得用JAVA语言来编写移动agent程序呢
- java.net.SocketException: Broken pipe 是什么原因引起的啊
- 『求助』Log4j的属性文件,logging.properties的内容是依照什么规范?谢谢
- 请问是不是 winxp 里自带的 IE6 不支持 Applet ?
- 怎样把网页中的APPLET背景由灰色改成别的颜色?透明?
- 菜鸟问题!请各位看看吧!
- 一道十分不解的JAVA问题
- js数字验证 请高手指教
- 在HashSet中搜索和删除的问题!!!!!!!!!!!!!!!
ArrayList<int[]> list = new ArrayList<int[]>();
while(scan.hasNext()) {
int[] nums = new int[4];
for(int num:nums)
num = scan.nextInt();
list.add(nums);
}
import java.io.FileReader;
import java.io.IOException;
public class SourceReader { /**
* @param args
*/
public static void main(String[] args) throws IOException {
int num=0;
FileReader fr=new FileReader("mydir1/mydir2/myfile.txt");
BufferedReader br=new BufferedReader(fr);
String s=br.readLine();
while(s!=null){
num++;
String[] ss=s.split(",");
System.out.println("第"+num+"行");
for(int i=0;i<ss.length;i++){
int a=Integer.parseInt(ss[i]);
System.out.print(" "+a);
}
System.out.println();
s=br.readLine();
}
}}
import java.io.*;public class SourceReader {
/**
* 返回的一个Map(已经排好序),Map中每个index值对应一个序列组。
*/
public static Map<Integer, ArrayList<int[]>> getData(File file) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(file))));
Map<Integer, ArrayList<int[]>> map = new TreeMap<Integer, ArrayList<int[]>>();
String message;
while((message = in.readLine()).length() > 0) {
String[] messages = message.split(",");
try {
int index;
if(!map.containsKey(index = Integer.parseInt(messages[0])))
map.put(index, new ArrayList<int[]>());
map.get(index).add(new int[]{Integer.parseInt(messages[1]), Integer.parseInt(messages[2])});
}
catch(IndexOutOfBoundsException ex) {
//TODO 写下当读取的一行出错时你想进行的操作
}
}
return map;
}
catch(IOException ex) {
//TODO 写下当出现IO异常时你想进行的操作
}
return null;
}
/**在这个方法里给了你的相关数据处理方式,如果你只需要得到数据的话,上面的方法返回的Map就可以了。你可以根据具体情况来处理Map。*/
public static void messages(Map<Integer, ArrayList<int[]>> map) {
//回答第一个问题
Map<Integer, Integer> map1 = new TreeMap<Integer, Integer>();
Set<Integer> set1 = map1.keySet();
Iterator<Integer> i1 = set1.iterator();
while(i1.hasNext()) {
int index = i1.next();
map1.put(index, map.get(index).size());
}
//这里的map1即为你所求
//回答第二个问题
//参数map即为所需要的,即第一个方法的返回值
//回答第三个问题
//不是很明白你的意思。如果说有2,3;3,4;2,3;3,4;3,5;3,4;(第一列和第四列省略),是不是返回最多的重复次数(3,4)的3次呢?
//回答第四个问题
//是不是 return map.size();
}
}