调用Array Java中的特定行
|
我正在尝试将文本文件加载到数组中,然后使用数组中的元素。
我的文本文件的格式为:
1 0 3 4 1
1 0 3 4 2
.....
2 2 2 2 2
我无法弄清楚如何在特定索引处提取不同的元素,以便可以使用它们或对它们进行数学运算。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Coord {
public int a,b,c,d,e,f;
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(\"/Users/evanlivingston/3a.txt\", true)));
Scanner sc = new Scanner(new File(\"/Users/evanlivingston/1.txt\"));
List<Coord> coords = new ArrayList<Coord>();{
// for each line in the file
while(sc.hasNextLine()) {
String[] numstrs = sc.nextLine().split(\"\\\\s+\");
Coord c = new Coord();
c.a = Integer.parseInt(numstrs[1]);
c.b = Integer.parseInt(numstrs[2]);
c.c = Integer.parseInt(numstrs[3]);
c.d = Integer.parseInt(numstrs[4]);
c.e = Integer.parseInt(numstrs[5]);
c.f = Integer.parseInt(numstrs[6]);
coords.add(c);
// now you have all coords in memory
for( int i=0; i<coords.size(); i++ ) {
// j=i+1 to calculate the distance between two points only once,
// not one way and back; also skip calculating distance between
// the same point
for( int j=i+1; j<coords.size(); j++ ) {
Coord c1 = coords.get(i);
Coord c2 = coords.get(j);
System.out.println(c2);
}
}
}
}
}
}
我主要关心的是执行类似从索引4的c.f中减去索引3的c.f的操作。
没有找到相关结果
已邀请:
2 个回复
峨躬坎抬焚
窃誓额