【发布时间】:2013-09-03 05:18:56
【问题描述】:
我的数据文件如下所示(第 1 列-x;第 2 列-y;第 3 类型):
46 80 2
95 75 2
78 85 2
59 54 1
81 52 2
57 78 1
72 46 2
我正在尝试将 x 和 y 坐标保存在两个不同的点数组列表中,具体取决于它们的类型。
import java.util.*;
import java.util.ArrayList;
import java.io.*;
import java.awt.Point;
public class program {
public static void main(String []args) {
ArrayList knots = new ArrayList<Point>();
ArrayList zeros = new ArrayList<Point>();
List<Integer> list = new ArrayList<Integer>();
String line = null;
String file = "hepatitis_data1.txt";
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
while((line = reader.readLine()) != null) {
String tmp[] = line.split(" +");
System.out.println(line);
for (String s:tmp) {
s = s.trim();
if(s.length() > 0) {
int i = Integer.parseInt(s.trim());
int r = Integer.parseInt(tmp[1].trim());
int g = Integer.parseInt(tmp[2].trim());
if (tmp[3].equals("1")) {
knots.add(new Point(r,g));
}
else if(tmp[3].equals("2")) {
zeros.add(new Point(r,g));
}
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int total = knots.size() + zeros.size();
System.out.println("knot size" + knots.size());
System.out.println("zero size" + zeros.size());
System.out.println("total size" + total);
}
}
它没有显示任何错误,但它也没有做正确的事情。总值应为 83,因为我有 83 对 x-y 坐标,但总数为 240。 有什么帮助吗??
【问题讨论】:
-
你确定吗?您是否调试并查看了列表中的内容?
-
是的,我尝试打印所有内容,但找不到错误。列表中没有任何内容,我忘记删除了。
-
if (tmp[3].equals("1") 中是tmp[3]还是tmp[2]?
标签: java text arraylist coordinates point