开发者

declaring an array within an array

开发者 https://www.devze.com 2023-04-06 19:30 出处:网络
I\'ve just started a Java programming class and I\'m having trouble setting up an array within an array.

I've just started a Java programming class and I'm having trouble setting up an array within an array.

Example:

public class ABLoop {
    int x;
    ABLoop[]Loop  = new ABLoop[x];
    int[]numb;

    public void fortime(int number){开发者_如何学编程

        x=number;

        for(int multiplier = 0; multiplier <= x; multiplier++){
            Loop[multiplier]= new Loop[multiplier+1];

For the new Loop, it keeps saying that Loop cannot be resolved to a type. Don't know what that means; can anyone offer suggestions? What I'm trying to do is for each element of Loop array, I want a new array created with elements equating to multiplier + 1.


This class will compile and run, but I have no idea what you're doing here.

public class ABLoop {

    int x;

    ABLoop[] loop;

    int [] numb;

    public ABLoop(int value) {
        if (value < 0)
            throw new IllegalArgumentException("value cannot be negative");

        this.x = value;
        this.loop = new ABLoop[this.x];
        this.numb = new int[this.x];  // no idea what you're doing here; just guessing
    }

    public void fortime() {

        for (int i = 0; i < this.x; ++i) {

            this.loop[i] = new ABLoop(i);  // What are you doing?  Just guessing.
        }
    }
}


I don't know if this is still relevant, but the last line:

Loop[multiplier]= new Loop[multiplier+1];

should be

Loop[multiplier]= new ABLoop[multiplier+1];

Loop is a variable and ABLoop is a type; new requires a type. Imagine

int[] a;
a = new a[7];

or

int[] a;
a = new int[7];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号