NhatNienne
NhatNienne

Reputation: 1061

Constructor getting an Array as parameter; How to use it

Well hi guys,

So my problem is that I'm having the following constructor:

int tuple[];
NaturalNumberTuple nnt;
public NaturalNumberTuple(int[] numbers) {
    int[] tuple = new int[numbers.length];
    for(int i : numbers){
        tuple[i] = numbers[i];
    }
    // TODO implement constructor
   // throw new UnsupportedOperationException("Constructor not yet implemented");
}

and now I'm trying to do the following task:

/**
 * Inserts the specified {@code number} at the end of this tuple. If {@code number} is smaller or equal to 0, this
 * method has no effect.
 * 
 * @param number
 *            the number to be inserted
 * @return the tuple resulting from inserting the specified {@code number}. If {@code number} is smaller or equal to
 *         0, this tuple is returned without any modifications.
 */
public NaturalNumberTuple insert(int number) {
    int placeholderTuple[] = new int[tuple.length+1];
    for(int i : tuple){
        placeholderTuple[i] = tuple[i];
        if(number > 0){
            placeholderTuple[placeholderTuple.length-1] = number;
        }
    } 
    return nnt.NaturalNumberTuple(placeholderTuple[]);
}

The Error is coming at my last row (return nnt. ....) Syntax error, insert ". class" to complete ArgumentList and The method NaturalNumberTuple(Class) is undefined for the type NaturalNumberTuple

So i thought why should I implement another class? I already got one called NaturalNumberTuple so I don't really know why this error is coming.. Moreover I got another Problem. I'm working with Arrays here as you can see and if I (for example) want to construct a new Tuple I'm using my Constructor but how am I passing my Array into it? You can look up my first try at the last row..

Sorry if those code samples are bad formatted and sorry for my bad english

Thanks anyways

Solved:

First Thank you Guys! I had to do the following: (for others who may have a similiar problem)

first in my Constructor I had to replace the line

int[] tuple = new int[numbers.length];

with

tuple = new int[numbers.length];

because I already defined my Array tuple

Second:

return nnt.NaturalNumberTuple(placeholderTuple[]);

with

return new NaturalNumberTuple(placeholderTuple);

Upvotes: 0

Views: 167

Answers (3)

WarrenFaith
WarrenFaith

Reputation: 57672

Beside the point from cello, you are messing up how your contructor works. The constructor is only called when you create a new instance with "new".

So replace that

return nnt.NaturalNumberTuple(placeholderTuple[]);

with

return new NaturalNumberTuple(placeholderTuple);

Upvotes: 0

SMA
SMA

Reputation: 37023

Couple of Issues:

  • change return nnt.NaturalNumberTuple(placeholderTuple[]); to return new NaturalNumberTuple(placeholderTuple);

You need to call constuctor with new operator and without "[]"

  • Change this line int[] tuple = new int[numbers.length]; to tuple = new int[numbers.length];

You are redefining tuple which when you will try to access from other instance method would leave with empty array.

Upvotes: 1

cello
cello

Reputation: 5486

placeholderTuple is defined as an array, so there is no need to add additional brackets [] when passing it as argument to a method.

So, the 2nd-last line should just be: return new NaturalNumberTuple(placeholderTuple);

Upvotes: 0

Related Questions