【问题标题】:Java varargs - why are these unacceptable syntax? [duplicate]Java varargs - 为什么这些不可接受的语法? [复制]
【发布时间】:2017-05-05 23:04:59
【问题描述】:

我为准备 OCAJP8 考试准备了这个练习测试题。有人可以解释为什么“错误”是错误的吗?谢谢。

Which of the following compile?

Response

Wrong   
public void moreD(String... values, int... nums) {}

Wrong   
public void moreF(String... values, int[] nums) {}

Correct 
public void moreB(String values, int... nums) {}

Correct 
public void moreG(String[] values, int[] nums) {}

Correct 
public void moreA(int... nums) {}

Wrong   
public void moreC(int... nums, String values) {}

【问题讨论】:

  • 一个方法只能有一个vararg参数,而且必须是最后一个参数。

标签: java compilation variadic-functions definitions signatures


【解决方案1】:

引用https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

可变参数只能用在最后的参数位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 2021-05-18
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2014-02-20
    • 1970-01-01
    相关资源
    最近更新 更多