Home > Enterprise >  Is there a way to subtract an array from an array?
Is there a way to subtract an array from an array?

Time:01-18

I have two arrays:

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [2, 4, 6, 8, 10]

How is it possible to make the output look like this?

list3 = [1, 3, 5, 7, 9]

list3[i] = list1[i] - list2[i];

Unfortunately this does not work for me because the two arrays are not same length.

import java.util.Arrays;
import java.util.Scanner;

public class Infinity {

    public static int[] InitialiseStudents(int[] students) {

        for (int i = 0; i<students.length; i  ){
            students[i] = i 1;
        }
        return students;
    }

    public static int[] AssignJobs (int[] NumberStudents) {
        int StudCount = NumberStudents.length;
        int[] Array = NumberStudents;

        //loop for every day(t), starting at the second day
        for (int t = 2; t <= StudCount; t  ) {
            System.out.println("Tag"   t);


            int[] copy = new int[5];

            for (int i = t-1, j=0; j < 5; i =t) {
                    copy[j  ] = Array[i];
            }
            System.out.println("-------------");
            System.out.println(Arrays.toString(copy));
            System.out.println("_________");
            break;
        }
        return Array;
    }

    public static void main( String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter Number of Students: ");
        //int n = scan.nextInt();
        int n = 10;

        int[] students = new int[n];

        InitialiseStudents(students);

        students = AssignJobs(students);
        System.out.println(Arrays.toString(students));

    }
}

To make to task VERY clear I just show you everything I have done and the context. This is my homework which I am curently working. The task is...

Initially, a numbering 1, 2, 3, 4, . . . of the students is determined. Then it will be that every second (start counting with the first student) becomes Garbage officer (these are the students with the numbers 2, 4, 6, 8, . . . ), from the rest (students 1, 3, 5, 7, . . . ) every third person becomes a refrigerator representative (these are students 5, 11, 17, 23, . . . ), of the rest (students 1, 3, 7, 9, . . . ) every fourth . . . , from which then we can think of something for each k-th and from the rest for every (k 1)-th etc. Apparently, some of the residents (students 1, 3, 7) omitted during distribution and do not have to complete any of the tasks that arise. Numbers of these students are called Omitted Numbers. Program an application OmittedNumbers that exactly the Omitted Numbers an area 1; : : : ;N retrieves and prints, where N is passed on the command line will. Only use methods that are iterative.

CodePudding user response:

Here's a simple implementation:

import java.util.ArrayList;
import java.util.List;

record ChoiceResult(List<Integer> chosen, List<Integer> remainder) {}

public class Choose {
    static ChoiceResult choose(int interval, List<Integer> candidates) {
        List<Integer> chosen = new ArrayList<>();
        List<Integer> remainder = new ArrayList<>();
        for (int i = 0; i < candidates.size(); i  ) {
            if ((i 1) % interval == 0) {
                chosen.add(candidates.get(i));
            } else {
                remainder.add(candidates.get(i));
            }
        }
        return new ChoiceResult(chosen, remainder);
    }

    public static void main(String[] args) {
        List<Integer> students = List.of(1,2,3,4,5,6,7,8,9,10);
        ChoiceResult garbage = choose(2, students);
        ChoiceResult fridge = choose(3, garbage.remainder());
        System.out.println("Garbage: "   garbage.chosen());
        System.out.println("Fridge: "   fridge.chosen());
    }
}

It has the feature of working with an immutable List for the input to the function.

CodePudding user response:

You can use double-layer for loop filtering,The following is just a sample code, you need to think about the details(For example, how to improve the efficiency of calculation, because the efficiency of double-layer for loop is very low).

public static void main(String[] args) {
    int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int[] arr2 = {2, 4, 6, 8, 10};

    List<Integer> rt = new ArrayList<>();
    for (int i = 0; i < arr1.length; i  ) {
        boolean flag = false;
        for (int j = 0; j < arr2.length; j  ) {
            if(arr1[i] == arr2[j]) {
                flag = true;
                break;
            }
        }
        if (flag == false) {
            rt.add(arr1[i]);
        }
    }
    System.out.println(rt);

    Integer[] finalArr = rt.toArray(new Integer[rt.size()]);

    for (int i = 0; i < finalArr.length; i  ) {
        System.out.println(finalArr[i]);
    }
}

CodePudding user response:

This one-liner should work for your needs.

let difference = list1.filter(x => !list2.includes(x));
  • Related