为什么我们不能将ArrayList 传递给带有(Integer ...)参数的方法?

时间:2021-07-31 21:02:46

I'm trying to do this Java How To Program Task: "Write an appliaction that calculates the product of a series of integers that are passed to method 'product' using a variable-length argument list.

我正在尝试这样做Java如何编程任务:“编写一个appliaction,计算使用可变长度参数列表传递给方法'product'的一系列整数的乘积。

I get the error message that the method product(Integer...) in the type VarLenArgumentList is not applicable for the arguments (ArrayList). Why so, if Java treats the variable-length argument list as an array? Isn't an ArrayList an array?

我得到错误消息,类型VarLenArgumentList中的方法产品(整数...)不适用于参数(ArrayList)。为什么如此,如果Java将可变长度参数列表视为数组? ArrayList不是一个数组吗?

What is another way of completing the task?

完成任务的另一种方法是什么?

Scanner keyboard = new Scanner(System.in);
int flag = 0;
ArrayList<Integer> intArray = new ArrayList<>();

do
{
    System.out.print("Enter a positive integer or '-1' to quit:" );
    int input = keyboard.nextInt();
    intArray.add(input);

} while (flag != -1);

product(intArray); 
}

public static int product (Integer... numbers) 
{
    int total = 0;

    for (Integer element : numbers)
        total *= element;

    return total;
}

2 个解决方案

#1


Integer... parameter accepts arbitrary number of Integer objects, or an array Integer[]. Since ArrayList<Integer> is not an Integer[], it is not accepted.

Integer ...参数接受任意数量的Integer对象或数组Integer []。由于ArrayList 不是Integer [],因此不接受它。

ArrayList is NOT an array, it is a Collection, while array in java is a different object.

ArrayList不是数组,它是Collection,而java中的数组是不同的对象。

You can however use toArray(T) method to easily turn your ArrayList into an array. but note that it will be a DIFFERENT object, and this is useful mainly when you just want to read from the collection, not write to it.

但是,您可以使用toArray(T)方法轻松地将ArrayList转换为数组。但请注意,它将是一个不同的对象,这主要是当你只想从集合中读取而不是写入它时。

#2


What is another way of completing the task?

完成任务的另一种方法是什么?

You can pass a List of Integer into the method.

您可以将List of Integer传递给方法。

 public static int product (List<Integer> integerList) 
    {
    Integer total = 0;

    for (Integer element : integerList)
        total *= element;

    return total;
    }

#1


Integer... parameter accepts arbitrary number of Integer objects, or an array Integer[]. Since ArrayList<Integer> is not an Integer[], it is not accepted.

Integer ...参数接受任意数量的Integer对象或数组Integer []。由于ArrayList 不是Integer [],因此不接受它。

ArrayList is NOT an array, it is a Collection, while array in java is a different object.

ArrayList不是数组,它是Collection,而java中的数组是不同的对象。

You can however use toArray(T) method to easily turn your ArrayList into an array. but note that it will be a DIFFERENT object, and this is useful mainly when you just want to read from the collection, not write to it.

但是,您可以使用toArray(T)方法轻松地将ArrayList转换为数组。但请注意,它将是一个不同的对象,这主要是当你只想从集合中读取而不是写入它时。

#2


What is another way of completing the task?

完成任务的另一种方法是什么?

You can pass a List of Integer into the method.

您可以将List of Integer传递给方法。

 public static int product (List<Integer> integerList) 
    {
    Integer total = 0;

    for (Integer element : integerList)
        total *= element;

    return total;
    }