gpt4 book ai didi

java - 从 Java 数组中调用特定行

转载 作者:行者123 更新时间:2023-11-30 05:04:04 26 4
gpt4 key购买 nike

我正在尝试将文本文件加载到数组中,然后使用数组中的元素。

我的文本文件的格式为:

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 之类的操作。

最佳答案

“我主要关心的是执行诸如从索引 4 的 c.f 中减去索引 3 的 c.f 之类的操作。”

Coord c1 = coords.get(3);
Coord c2 = coords.get(4);

int foo = c2.f - c1.f;

关于java - 从 Java 数组中调用特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5709838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com