2016-11-17 56 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);