【问题标题】:How to subtract values of two lists/arrays in Java?如何在Java中减去两个列表/数组的值?
【发布时间】: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


【解决方案1】:

使用 Stream 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);

【讨论】:

    【解决方案2】:

    它非常简单。在解决编程问题之前。始终按步骤进行。以你的问题为例。您需要从 2 个带有值的数组中减去值。

    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]);
        }
      }
    

    }

    【讨论】:

    • 您应该添加检查以确保 list1 和 list2 的大小相同。但你明白了。
    • ich habe es davor schon mal so gehabt jedoch hat es mir "java.lang.NullPointerException" ausgespuckt: int[] results = null; for (int i = 0; i
    • 不要写空。写类似 int[] results = new int[anf.length];
    • 仍然以“java.lang.NullPointerException”结束
    • 基本上你想从array或arrayList中减去它?
    【解决方案3】:

    这是一个非常简单的答案,您可以将其应用到您的程序中。

    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]);
        }
    }
    

    【讨论】:

      【解决方案4】:

      答案是Collection类的removeAll方法

      ArrayList<Integer> s1 = new ArrayList<Integer>(Arrays.asList(a1));
      ArrayList<Integer> s2 = new ArrayList<Integer>(Arrays.asList(a2));
      s1.removeAll(s2);
      Integer[] result = s1.toArray(new Integer[s1.size()]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多