我正在开发一个Java项目,并遇到问题。我想在列表/数组c中减去两个列表或数组a和b,但我不知道该怎么做。我希望“a [i] -b [i]”应该在下一个列表c中,其值应该是c[i]
类似地对于5-2 = 3任何建议和帮助将不胜感激。如何减去Java中两个列表/数组的值?
代码:
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队友 – Voodoothechild
使用要么罚款。如果你期望两组数据总是具有相同的大小,我可能会倾向于一个数组。 –