我正在開發一個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
使用要麼罰款。如果你期望兩組數據總是具有相同的大小,我可能會傾向於一個數組。 –