Brent
Brent

Reputation: 29

declaring 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.

Upvotes: 2

Views: 254

Answers (2)

Heimdall
Heimdall

Reputation: 237

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];

Upvotes: 0

duffymo
duffymo

Reputation: 309008

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.
        }
    }
}

Upvotes: 2

Related Questions