JAVA,以可变数量的对象作为参数的数组
我正在尝试获取一个数组以将可变数量的对象作为输入。我是编程新手,所以我提前道歉。
这是我的代码:
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};
}
}
但是,我收到与 double[] All_users={Rating..arguments); 相关的错误:
Multiple markers at this line
- Syntax error on token "...", . expected
- arguments cannot be resolved or is
not a field
有什么想法吗?提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题出在您的
All_user
类构造函数中;您正在尝试将Ratings[]
类型的...某物...设置为double[]
类型的类成员您可以执行以下操作之一:
1 - 让
All_user
的构造函数接收一个作为Rating
实例的数组(或可变长度参数),然后将其简单地分配给相同的类成员(一个数组)输入:或从每个
Rating
收集所有值(double ratings
)并将它们映射到一个数组中,我认为后者就是您想要做的。另请注意,您的
Rating
类也可以重写为仅供参考:可变长度参数始终是声明的参数中的最后一个。有关 varargs 的更多信息此处和此处。
The problem comes in your
All_user
class 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 ofRating
and simply assign it to the class member (an array) of the same type :or collect all values (
double Ratings
) from eachRating
and map them into an arrayI think the latter is what you are trying to do. Also, note that your
Rating
class could also be rewritten asFYI: The variable-length argument is always the last one in the declared arguments. More on varargs here and here.
可变长度参数(var-args)像数组一样被访问。假设您要将所有用户的所有评分扁平化到
All_user
中的一个double[]
中,则:Variable length args (var-args) are accessed like an array. Assuming you want to flat-pack all ratings of all users into a single
double[]
inAll_user
, then:argument
表示为在此上下文中使用的Rating
对象数组。不过,使用 varargs 时要非常小心。除非您要创建一个库,否则这很少是一个好的设计决策。argument
is represented as an array ofRating
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.当您使用 vargs 表示法时,arg 将作为数组处理。在您的情况下,它将是一个
Rating
数组。您可以在方法主体中引用它,就像引用数组一样,即就像将其声明为arguments[]
一样。一些批评:
Rating
转换为double
,因此您已经遇到了麻烦。...
表示法仅在方法的声明中使用,而不是引用方法体内的 arg 。argument
时,它看起来也好像将您的 arg 引用为arguments
。Rating...
和 arg 名称之间添加一个空格。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 asarguments[]
.A couple critiques:
Rating
to adouble
, so you are already running into trouble....
notation is only used in the declaration of the method, not to refer to the arg within the method body.arguments
, when you declare it asargument
.Rating...
and the name of the arg.