如何在方法之间进行交集

How to make intersection between method

我的代码的输出是:

The fibonacci list that smaller than 40 is:
0 1 1 2 3 5 8 13 21 34 
The prime list that smaller than 40 is:
2 3 5 7 11 13 17 19 23 29 31 37

我想在这两个列表之间创建交集。

使其变为:(当我将变量 n=40 放入 fibo() 和 allPrime() 方法中时)

2 3 5 13 

但我不知道该怎么做。我搜了下论坛,大部分交集问题是在两个arraylist或者两个set之间。

我想知道是否可以像这样在两个函数之间建立交集?

public class FiboAndPrime {

    static boolean IsPrime(int n) {
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    // Find all the prime numbers that are less than or equal to n
    static void allPrime(int n) {
        for(int i=2; i<=n; i++) {
            if(IsPrime(i)) System.out.print(i+ " ");
        }
    }

    //Find the Fibonacci numbers that are less than or equal to n
    static void fibo(int n) {
        int fibo[] = new int[n];
        fibo[0] = 0;
        fibo[1] = 1;
        System.out.print(fibo[0]+" "+fibo[1]+" ");
        for (int i = 2; i <= n; i++) {
            fibo[i] = fibo[i - 1] + fibo[i - 2];
            if (n >= fibo[i]) {
                System.out.print(fibo[i]+ " ");
            } else {
                break;
            }
        }
    }

    public static void main(String[] args) {
        int k = 40;
        System.out.println("The fibonacci list that smaller than " + k + " is:");
        fibo(k);
        System.out.println();
        System.out.println("The prime list that smaller than " + k + " is:");
        allPrime(k);
    }

}

我试图更改我的代码以使用 ArrayList,但我被困在 fibo() 方法上。

输出为:

The final intersection that are both fabonacci and prime is:
0 1true true true true true true true true 
The prime list that smaller than 40 is:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

为什么它在我的斐波那契列表中变成布尔类型?

static void allPrime(int n) {
    List<Integer> primes = new ArrayList<Integer>(n);
    for(int i=2; i<=n; i++) {
        if(IsPrime(i)) {
            primes.add(i);
        }
    }
    System.out.print(primes);
}

static void fibo(int n) {
    List <Integer> fibos = new ArrayList<>(n);
    int fibo[] = new int[n];
    fibo[0] = 0;
    fibo[1] = 1;
    System.out.print(fibo[0]+" " + fibo[1]);
    for (int i = 2; i <= n; i++) {
        fibo[i] = fibo[i - 1] + fibo[i - 2];
        if (n >= fibo[i]) {
           int in =fibo[i];
           System.out.print(fibos.add(in)+ " ");
        } else {
            break;
        }
    }
}

您将需要使用像 HashSetArrayList 这样的数据结构来执行此操作,然后找到它们之间的交集。

使用 ArrayList 的解决方案:

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

public class FiboAndPrime {


    static boolean IsPrime(int n) {
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
    // Find all the prime numbers that are less than or equal to n
    static void allPrime(int n, List<Integer> prime_set ) {
        for(int i=2; i<=n; i++) {
            if(IsPrime(i)) System.out.print(i+ " ");
            prime_set.add(i);
        }
    }
    //Find the Fibonacci numbers that are less than or equal to n
    static void fibo(int n, List<Integer> fibo_set ) {
        int fibo[] = new int[n];
        fibo[0] = 0;
        fibo[1] = 1;
        System.out.print(fibo[0]+" "+fibo[1]+" ");
        for (int i = 2; i <= n; i++) {
            fibo[i] = fibo[i - 1] + fibo[i - 2];
            if (n >= fibo[i]) {
                System.out.print(fibo[i]+ " ");
                fibo_set.add(fibo[i]);
            } else {
                break;
            }
        }
    }


    public static void main(String[] args) {
        int k = 40;


        System.out.println("The fibonacci list that smaller than " + k + " is:");
        List<Integer> fibo_set = new ArrayList<Integer>();
        fibo_set.add(0);
        fibo_set.add(1);
        List<Integer> prime_set = new ArrayList<Integer>();
        fibo(k,fibo_set);
        System.out.println();
        System.out.println("The prime list that smaller than " + k + " is:");
        allPrime(k,prime_set);

    fibo_set.retainAll(prime_set); // fibo_set now contains only elements in both sets

    System.out.println();
    System.out.println("intersection between the fibo and prime set:");
    for (Integer intersection : fibo_set) {
    System.out.println(intersection);
    }

    }
}

使用 HashSet 的解决方案:

import java.util.Set;
import java.util.HashSet;

public class FiboAndPrime {


    static boolean IsPrime(int n) {
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
    // Find all the prime numbers that are less than or equal to n
    static void allPrime(int n, Set<Integer> prime_set ) {
        for(int i=2; i<=n; i++) {
            if(IsPrime(i)) System.out.print(i+ " ");
            prime_set.add(i);
        }
    }
    //Find the Fibonacci numbers that are less than or equal to n
    static void fibo(int n, Set<Integer> fibo_set ) {
        int fibo[] = new int[n];
        fibo[0] = 0;
        fibo[1] = 1;
        System.out.print(fibo[0]+" "+fibo[1]+" ");
        for (int i = 2; i <= n; i++) {
            fibo[i] = fibo[i - 1] + fibo[i - 2];
            if (n >= fibo[i]) {
                System.out.print(fibo[i]+ " ");
                fibo_set.add(fibo[i]);
            } else {
                break;
            }
        }
    }


    public static void main(String[] args) {
        int k = 40;


        System.out.println("The fibonacci list that smaller than " + k + " is:");
        Set<Integer> fibo_set = new HashSet<Integer>();
        fibo_set.add(0);
        fibo_set.add(1);
        Set<Integer> prime_set = new HashSet<Integer>();
        fibo(k,fibo_set);
        System.out.println();
        System.out.println("The prime list that smaller than " + k + " is:");
        allPrime(k,prime_set);

    fibo_set.retainAll(prime_set); // fibo_set now contains only elements in both sets

    System.out.println();
    System.out.println("intersection between the fibo and prime set:");
    for (Integer intersection : fibo_set) {
    System.out.println(intersection);
    }

    }
}

绝对有可能。我不会为您编写代码,但我可以建议您应该如何做。与其打印 40 以下的斐波那契数列和素数,不如将它们存储在 2 个不同的数组中,并尝试找出 them.But 之间的共同点我认为你不熟悉数组列表和集合,我会建议你另一种方法。

当您找到 40 以下的斐波那契数时,请检查该数是否为质数。如果是,则将其打印出来,否则就不要打印。

为什么在 java 中使用 array 而不是 list。你可以简单地通过 retainAll 实现,如下所示:

    List<Integer> ret = new ArrayList<>(primes);
    ret.retainAll(fibos);

本地测试:

public static void main(String... args) {
    List<Integer> primes = new ArrayList<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37));
    List<Integer> fibos = new ArrayList<>(Arrays.asList(0, 1, 1, 2, 3, 5, 8, 13, 21, 34));
    List<Integer> ret = new ArrayList<>(primes);
    ret.retainAll(fibos);
    System.out.println(ret);
}

输出将是:

[2, 3, 5, 13]

已更新

应OP的要求,我补充一下以供参考:

static void fibo(int n) {
    List <Integer> fibos = new ArrayList<>(n);
    fibos.add(0);
    fibos.add(1);
    System.out.print(fibos.get(0) + "," + fibos.get(1));
    for (int i = 2; i <= n; i++) {
        int a = fibos.get(i-1) + fibos.get(i-2);
        if (a <= n) {
            fibos.add(a);
            System.out.print("," + a);
        } else break;
    }
}