简体   繁体   English

数组隐私Java-基本

[英]Array Privacy Java - Basic

I'm sorry for (another) admittedly quite basic question, but I'm having more problems in some Java code that I have to right for class. 对于另一个问题,我很抱歉,这是一个非常基本的问题,但是我在某些Java代码中遇到了更多的问题,这些问题我必须适合上课。 The assignment is to do a sieve of Eratosthenes program, and while my constructor seems to work (calling myArray[50] from the main method gives the value it was initialized to), it doesn't reflect any changes that the other classes were meant to make to myArray . 分配工作是对Eratosthenes程序进行筛分,尽管我的构造函数似乎可以工作(从main方法调用myArray[50]给出了初始化的值),但是它没有反映其他类的含义的任何更改。制作myArray Also, print() prints nothing (probably because the function sees an unitialized version of myArray .). 另外, print()打印任何内容(可能是因为该函数看到了myArray的统一版本)。

The goal is that SieveEm() and deleteStuff() mark all of the non-primes as false, the print() comes by and prints all of the prime numbers(marked true). 目标是SieveEm()和deleteStuff()将所有非素数标记为false,print()随之出现并打印所有素数(标记为true)。

I couldn't seem to find anything on the internet/StackOverflow addressing the problem. 我似乎无法在Internet / StackOverflow上找到任何解决该问题的方法。 What am I doing wrong? 我究竟做错了什么?

    public class Sieve {
    private int count;
    private boolean[] myArray;
    public Sieve(int length){
        count = length+1;
        boolean[] newArray = new boolean[length+1];
        if(length>=1){
            newArray[0]=false;
            newArray[1]=false;
            if(length>=2){
                for(int i=0;i<=length;i++){
                    newArray[i]=true;
                }
            }
        }
        this.myArray = newArray;
    }
    public void SieveEm(){
        System.out.println("here now");
        System.out.println(this.myArray[50]==true);
        for(int i=0; i<count;i++){
            System.out.println("We got this far");
            if(this.myArray[i]){
                deleteStuff(i);
            }
        }
    }
    public void deleteStuff(int current){
        int increment= current;
        current+=increment;
        while(current<count){
            this.myArray[current]=false;
            current+=increment;
        }
    }
    public void print(){
        this.SieveEm();
        for(int i=2;i<count;i++){
            if(this.myArray[i]){
                System.out.println(i);
            }
        }
    }
    public static void main(String[] args){
        Sieve mySieve = new Sieve(100);
        mySieve.print();
        System.out.println(mySieve.myArray[50]);
    }
}

I will go method by method for anything that has issues because there seems to be a bit that needs correcting. 对于有问题的任何事情,我都会逐一处理,因为似乎有些地方需要纠正。

For public void deleteStuff(int current), this int increment= current; current+=increment; 对于public void deleteStuff(int current),int increment= current; current+=increment; int increment= current; current+=increment; seems rather redundant and hurtful because increment does not do anything special. 似乎相当多余和痛苦,因为increment并没有做任何特别的事情。 Even more importantly, your while - loop will never terminate because on the first pass current = 0; 更重要的是,您的while - loop将永远不会终止,因为在第一次通过时current = 0; and increment = 0; increment = 0; . If you do current++; 如果您current++; then your while-loop WILL terminate. 然后您的while-loop将终止。 Also, if you while loop does progress and goes through the array, all true values will be set to false. 另外,如果while循环确实在执行并遍历数组,则所有true值都将设置为false。

Onto print() . print() This method does not print anything because you set the entire myArray to false. 此方法不打印任何内容,因为您将整个myArray设置为false。 Of course, because of the while-loop issue I mentioned earlier, the for-loop to print is never reached because you are stuck in a never ending while-loop. 当然,由于我前面提到的while-loop问题,因为您陷入了永无休止的while-loop. ,所以永远无法达到要打印的for-loop while-loop. So, if you solve the while - loop issue, you set every element in the array to false so the statment below never executes. 因此,如果您解决了while - loop问题,则可以将数组中的每个元素设置为false,这样就永远不会执行下面的语句。

if(this.myArray[i]) { System.out.println(i); }

If I notice anything else I will let you know. 如果我发现其他任何问题,我会通知您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM