【发布时间】:2016-11-17 00:53:55
【问题描述】:
我正在处理一个 Java 项目,但遇到了问题。我想在列表/数组 c 中减去两个列表或数组 a 和 b,但我不知道该怎么做。我希望“a[i] -b[i]”应该在下一个列表 c 中,其中值应该是 c[i] 类似地对于 5-2=3 任何建议和帮助将不胜感激。
代码:
public static void länge() {
{
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File("C:\\Users/Voodoothechild/Desktop/Pidata/Anfang.txt")));
String line = null;
while ((line = br.readLine()) != null) {
{
BufferedReader bro = null;
try {
bro = new BufferedReader(new FileReader(new File("C:\\Users/Voodoothechild/Desktop/Pidata/Ende.txt")));
String lines = null;
while ((lines = br.readLine()) != null) {
String[] anfang = line.split("\n");
String[] ende = lines.split("\n");
List<Integer> an = new ArrayList<Integer>();
List<Integer> en = new ArrayList<Integer>();
for (int index = 0 ; index<ende.length ; index++) {
an.add(Integer.parseInt(anfang[index]));
en.add(Integer.parseInt(ende[index]));
Integer[] anf = new Integer[an.size()];
int[] result = new int[anf.length];
for (int i = 0; i < result.length; i++) {
result[i] = anf[i] - end[i];
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bro != null) {
try {
bro.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
【问题讨论】:
-
请修正您的代码格式,这只会帮助您解决问题。顺便说一句,循环遍历每一对或元素并减去是我的做法。
-
我应该循环一个列表还是一个数组?和 tnx 伴侣
-
使用任何一个都可以。如果您希望两组数据始终具有相同的大小,我可能会倾向于使用数组。
标签: java arrays list subtraction