Voodoothechild
Voodoothechild

Reputation: 31

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

I am working on a Java project and I am having a problem. I want to have the subtract of two lists or arrays a and b in list/array c but I don't know how to do that. I want "a[i] -b[i]" should be in next list c where the value should be c[i] similarly for 5-2=3 any suggestion and help would be appreciated.

Code:

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

Upvotes: 2

Views: 30888

Answers (5)

Hasindu Dahanayake
Hasindu Dahanayake

Reputation: 1491

public static void findMinus(int[] arr1, int[] arr2) {
        int[] minusResult;
        if (arr1.length < arr2.length) {
            minusResult = new int[arr2.length];
            for (int i = 0; i < arr1.length; i++) {
                minusResult[i] = arr1[i] - arr2[i];

            }
            for (int i = arr1.length; i < arr2.length; i++) {
                minusResult[i] = arr2[i];
            }
        } else {
            minusResult = new int[arr1.length];
            for (int i = 0; i < arr2.length; i++) {
                minusResult[i] = arr1[i] - arr2[i];
            }
            for (int i = arr2.length; i < arr1.length; i++) {
                minusResult[i] = arr1[i];
            }
        }
        //Displaying the output
        for (int i : minusResult) {
            System.out.println(i);
        }

    }

Upvotes: -1

jlbofh
jlbofh

Reputation: 442

The answer is the method removeAll of class Collection

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

Upvotes: 0

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13773

It can be done easily using 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());

or even easier using Javaslang:

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

or by zipping two lists together:

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

Upvotes: 4

Aurora_Titanium
Aurora_Titanium

Reputation: 472

Its pretty straight forward. Before tackling a programming problem. Always do it in steps. Take your problem for example. You need to subtract values from 2 arrays with values.

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

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

Now you have 2 list. Ok next step, write a loop to go through the list. But first make sure that both list have the same length or else you will get an index out of bounce.

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

Anyway here is the full answer. Be sure to understand it

public class subtract {

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

}

Upvotes: 1

m4heshd
m4heshd

Reputation: 970

Here's a very simple answer you can apply to your program.

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

Upvotes: 0

Related Questions