你可以在java中的变量中添加一个数组列表吗?

时间:2015-03-29 00:40:17

标签: java arrays variables arraylist bluej

我有以下代码:

public static void main (String args[])
{

    Scanner input = new Scanner(System.in);
    System.out.println("\nLab1a\n");
    final int MAX = 100;
    boolean primes[];
    primes = new boolean[MAX];


    ArrayList<Integer>PrimeFactor = new ArrayList<Integer>();
    for (int i = 1; i < MAX + 1; i++)
    {
       PrimeFactor.add(i);
    }

    System.out.println("Computing Prime Numbers");
    System.out.println();
    System.out.println("Primes Between 1 and 100");
    System.out.println(PrimeFactor);
}

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
    for (int i = 0; i < PrimeFactor.size(); i++)
    {
        if (isPrime(PrimeFactor.get(i)))
        { PrimeFactor.remove(i);}
    }
}

public static boolean isPrime(int n)
{
    for (int i = 2; i < n; i++)
    {
        if (n%i == 0)
          return false;
    }
    return true;
}

}

所以我想做的是输出数字1到100的素数。问题是每当我输出时,我只得到数字1到100.我怎么能解决这个问题?我看到问题是因为PrimeFactor被用于两个不同的事情,我不确定。所以请帮忙。

3 个答案:

答案 0 :(得分:1)

除了Dev先生的代码,我想补充一下。

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
    for (int i = 0; i < PrimeFactor.size(); i++)
    {
        if (!isPrime(PrimeFactor.get(i)))
        { 
            PrimeFactor.remove(i);
            i--;  //<--

        }
    }
}

删除项目后,数组列表将缩小。一旦数组调整,此递减将在i处重新检查元素。

答案 1 :(得分:0)

答案。

public static void main (String args[])
{

    Scanner input = new Scanner(System.in);
    System.out.println("\nLab1a\n");
    final int MAX = 100;
    boolean primes[];
    primes = new boolean[MAX];

    ArrayList<Integer>PrimeFactor = new ArrayList<Integer>();
    for (int i = 2; i < MAX + 1 ; i++)
    {
        PrimeFactor.add(i);
    }

    CompositeNumbers(PrimeFactor);
    System.out.println("COMPUTING RIME NUMBERS");
    System.out.println();
    System.out.println("PRIMES BETWEEN 1 AND 100");
    CompositeNumbers(PrimeFactor);
    System.out.println(PrimeFactor);
}

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
    for (int i = 0; i < PrimeFactor.size(); i++)
    {
        if (!isPrime(PrimeFactor.get(i)))
        { 
            PrimeFactor.remove(i);
            i--;
        }
    }
}

public static boolean isPrime(int n)
{
    if(n==1)
    {
        return true;
    }
    for (int i = 2; i < n +1/2; i++)
    {
        if (n%i == 0)
        {
            return false;
        }
    }
    return true;
}

}

答案 2 :(得分:-1)

你应该致电

CompositeNumbers(PrimeFactor);

在您的主要打印PrimeFactor

public static void main (String args[])
{

    Scanner input = new Scanner(System.in);
    System.out.println("\nLab1a\n");
    final int MAX = 100;
    boolean primes[];
    primes = new boolean[MAX];


    ArrayList<Integer>PrimeFactor = new ArrayList<Integer>();
    for (int i = 1; i < MAX + 1; i++)
    {
       PrimeFactor.add(i);
    }

    CompositeNumbers(PrimeFactor);
    System.out.println("Computing Prime Numbers");
    System.out.println();
    System.out.println("Primes Between 1 and 100");
    System.out.println(PrimeFactor);
}

public static void CompositeNumbers(ArrayList<Integer> PrimeFactor)
{
    for (int i = 0; i < PrimeFactor.size(); i++)
    {
        if (!isPrime(PrimeFactor.get(i)))
        { 
            PrimeFactor.remove(i);
        }
    }
}

public static boolean isPrime(int n)
{
    if(n==1)
    {
        return true;
    }
    for (int i = 2; i < n+1/2; i++)
    {
        if (n%i == 0)
        {
            return false;
        }
    }
    return true;
}