JAVA,以可变数量的对象作为参数的数组

发布于 2024-09-18 15:39:14 字数 695 浏览 6 评论 0原文

我正在尝试获取一个数组以将可变数量的对象作为输入。我是编程新手,所以我提前道歉。

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

表情可笑 2024-09-25 15:39:14

问题出在您的 All_user 类构造函数中;您正在尝试将 Ratings[] 类型的...某物...设置为 double[] 类型的类成员

您可以执行以下操作之一:

1 - 让 All_user 的构造函数接收一个作为 Rating 实例的数组(或可变长度参数),然后将其简单地分配给相同的类成员(一个数组)输入:

public class All_user{
    Rating [] All_users;

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

或从每个 Rating 收集所有值(double ratings)并将它们映射到一个数组中,

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);
    }
}

我认为后者就是您想要做的。另请注意,您的 Rating 类也可以重写为

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

仅供参考:可变长度参数始终是声明的参数中的最后一个。有关 varargs 的更多信息此处此处

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.

白鸥掠海 2024-09-25 15:39:14

可变长度参数(var-args)像数组一样被访问。假设您要将所有用户的所有评分扁平化到 All_user 中的一个 double[] 中,则:


public class All_user{
    double [] All_users;
    public All_user(Rating... argument) {
        int totalSize = 0;
        for(Rating r:argument)
            totalSize+=r.getRatings().length; //double[] Ratings
        double [] All_users= new double[totalSize];
        int index = 0;
        for(Rating r:argument){
            for(double r:r.getRatings()){
                 All_users[index] = r;
                 index++;
            }
        }
        ...
    }
}

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[] in All_user, then:


public class All_user{
    double [] All_users;
    public All_user(Rating... argument) {
        int totalSize = 0;
        for(Rating r:argument)
            totalSize+=r.getRatings().length; //double[] Ratings
        double [] All_users= new double[totalSize];
        int index = 0;
        for(Rating r:argument){
            for(double r:r.getRatings()){
                 All_users[index] = r;
                 index++;
            }
        }
        ...
    }
}

小瓶盖 2024-09-25 15:39:14

argument 表示为在此上下文中使用的 Rating 对象数组。不过,使用 varargs 时要非常小心。除非您要创建一个库,否则这很少是一个好的设计决策。

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.

你另情深 2024-09-25 15:39:14

当您使用 vargs 表示法时,arg 将作为数组处理。在您的情况下,它将是一个 Rating 数组。您可以在方法主体中引用它,就像引用数组一样,即就像将其声明为 arguments[] 一样。

一些批评:

  1. 您将无法将您的 Rating 转换为 double,因此您已经遇到了麻烦。
  2. 关于您的错误,首先要注意的是 ... 表示法仅在方法的声明中使用,而不是引用方法体内的 arg 。
  3. 当您将 arg 声明为 argument 时,它看起来也好像将您的 arg 引用为 arguments
  4. 此外,您应该在 varg 声明 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 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文