2

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.

2
  • 1
    This code is utterly broken in almost every single respect; it's difficult to know where to start. Please explain more clearly what you are trying to do. Commented Sep 20, 2011 at 22:59
  • Are you trying to increase the capacity of the internal array and maintain the existing array elements? Commented Sep 20, 2011 at 23:04

2 Answers 2

2

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.
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.