views:

137

answers:

5

I am trying to get an array to take a variable number of objects as input. I am new to programming so I apologize in advance.

Here is my code:

public class Rating{
    double [] Ratings;
    int CustomerID;
    int Domain;

    public Rating (int id, int d, double [] x) {
        double [] Ratings = x;
        int CustomerID=id;
        int Domain=d;
    }
}

public class All_user{
    double [] All_users;

    public All_user(Rating...argument) {
        double [] All_users={Rating...argument};
    }
}

However, I am get this error associated with double[] All_users={Rating..arguments);:

    Multiple markers at this line
 - Syntax error on token "...", . expected
 - arguments cannot be resolved or is 
  not a field

Any thoughts? Thanks in advance!

A: 

Check out this tutorial for some insight. It looks like it may be of assistance.

Also, see this SO Question

Dave McClelland
A: 

argument is represented as an array of Rating objects, used in this context. Be really careful of using varargs though. It's rarely a good design decision unless you're making a library.

Mike
+1  A: 
badroit
A: 

When you use the vargs notation, the arg is handled as an array. In your case, it would be an array of Rating. You can reference it within the method body as you would an array, ie as if it was declared as arguments[].

A couple critiques:

  1. You will not be able to cast your Rating to a double, so you are already running into trouble.
  2. With regard to your error, the first thing to note is that the ... notation is only used in the declaration of the method, not to refer to the arg within the method body.
  3. It also looks as though you are referring to your arg as arguments, when you declare it as argument.
  4. Further, you should add a space between your varg declaration Rating... and the name of the arg.
akf
+2  A: 

The problem comes in your All_user class constructor; you're trying to set... something... of type Ratings[] to a class member of type double[]

You could do one of the following :

1- Have your All_user's constructor receive an array (or variable-length arguments) be instances of Rating and simply assign it to the class member (an array) of the same type :

public class All_user{
    Rating [] All_users;

    public All_user(Rating...argument) {
        All_users = argument;  // arguments is a Rating[]
    }
}

or collect all values (double Ratings) from each Rating and map them into an array

public class All_user{
    double [] All_users;

    public All_user(Rating...argument) {
        ArrayList<Double> ratings = new ArrayList<Double>();
        for (Rating r : argument) {
            for (double d : r.Ratings) ratings.add(d);
        }
        All_users = new double[ratings.size()];
        for (int i=0; i<ratings.size(); i++) All_users[i] = ratings.get(i);
    }
}

I think the latter is what you are trying to do. Also, note that your Rating class could also be rewritten as

public class Rating{
    double [] Ratings;
    int CustomerID;
    int Domain;

    public Rating (int id, int d, double...x) {
        double [] Ratings = x;
        int CustomerID=id;
        int Domain=d;
    }
}

FYI: The variable-length argument is always the last one in the declared arguments. More on varargs here and here.

Yanick Rochon
Primitives such as double can not be a generic type arg in Java.
ILMTitan
dang! didn't do Java for a while... it's not as pretty as it could be, but I fixed that. Thanks.
Yanick Rochon