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!
The problem comes in your
All_userclass constructor; you’re trying to set… something… of typeRatings[]to a class member of typedouble[]You could do one of the following :
1- Have your
All_user‘s constructor receive an array (or variable-length arguments) be instances ofRatingand simply assign it to the class member (an array) of the same type :or collect all values (
double Ratings) from eachRatingand map them into an arrayI think the latter is what you are trying to do. Also, note that your
Ratingclass could also be rewritten asFYI: The variable-length argument is always the last one in the declared arguments. More on varargs here and here.