簡體   English   中英

如何在Java中減去兩個列表/數組的值?

[英]How to subtract values of two lists/arrays in Java?

我正在開發Java項目,但遇到了問題。 我想在列表/數組c中減去兩個列表或數組a和b,但是我不知道該怎么做。 我希望“ a [i] -b [i]”應在下一個列表c中,對於5-2 = 3,其值應類似為c[i] ,任何建議和幫助將不勝感激。

碼:

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

它非常簡單。 在解決編程問題之前。 務必分步進行。 以您的問題為例。 您需要從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]);
    }
  }

}

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

您可以將其應用到程序中,這是一個非常簡單的答案。

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

答案是類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()]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM