2016-11-17 77 views
0

我正在開發一個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(); 
       } 
      } 
     } 
    } 
} 
+0

請修復你的代碼的格式,這隻會幫助你解決你的問題。順便說一下,循環每對或元素和減法是我會如何做到這一點。 –

+0

我應該循環一個列表還是數組?和TNX隊友 – Voodoothechild

+0

使用要麼罰款。如果你期望兩組數據總是具有相同的大小,我可能會傾向於一個數組。 –

回答

0

它非常直截了當。在解決編程問題之前。始終一步一步做。以你的問題爲例。您需要從兩個數組中減去值。

int [] list1 = {4,2,1};

int [] list2 = {2,2,-1};

現在你有2個列表。好的下一步,寫一個循環遍歷列表。但首先要確保兩個列表具有相同的長度,否則您將得到一個反彈的索引。

for(int i =0; i< list1.length; i++) 
    { 
     list3[i] = list1[i] - list2[i]; 
    } 

無論如何,這裏是完整的答案。一定要了解它

公共類減 {

public static void main(String[] args) 
    { 
    int[] list1 = {4,2,1}; 
    int[] list2 = {2,2,-1}; 
    int[] list3 = new int[list1.length]; 

    //Looping the list 
    for(int i =0; i< list1.length; i++) 
    { 
     list3[i] = list1[i] - list2[i]; 
    } 

    //Print Statement 
    for(int j =0; j< list3.length; j++) 
    { 
     System.out.println(list3[j]); 
    } 
    } 

}

+0

您應該添加檢查以確保list1和list2的大小相同。但你明白了。 –

+0

ich habe es davor schon mal so gehabt jedoch hat es mir「java.lang.NullPointerException」ausgespuckt:int [] results = null; \t \t \t \t \t \t \t \t \t \t \t對(INT I = 0;我 Voodoothechild

+1

不寫入null。寫一些像int [] results = new int [anf.length]; –

0

這裏是一個非常簡單的答案,你可以應用到你的程序。

public static void main(String[] args) { 
    int[] a = new int[4]; 
    int[] b = new int[4]; 
    int[] c = new int[4]; 

    a[0] = 5; 
    a[1] = 12; 
    a[2] = 20; 
    a[3] = 50; 
    b[0] = 1; 
    b[1] = 2; 
    b[2] = 3; 
    b[3] = 4; 

    for(int i=0; i<a.length; i++){ 
     c[i] = a[i] - b[i]; 
     System.out.println(c[i]); 
    } 
} 
1

它可以很容易地使用流API來完成:

int[] list1 = {4,2,1}; 
int[] list2 = {2,2,-1}; 

IntStream.range(0, list1.length) 
    .mapToObj(i -> list1[i] - list2[i]) 
    .collect(Collectors.toList()); 

或更容易使用Javaslang:

Stream.range(0, list1.length) 
    map(i -> list1[i] - list2[i]); 

或拉上兩個列表一起:

List.ofAll(list1) 
    .zip(List.ofAll(list2)) 
    .map(t -> t._1 - t._2);