【问题标题】:The type of the expression must be an array type but it resolved to List<String>表达式的类型必须是数组类型,但它解析为 List<String>
【发布时间】:2016-02-20 16:17:24
【问题描述】:

我收到这条消息:

表达式的类型必须是数组类型,但它解析为 列表

错误出现在这部分for循环中:

for(int i = 0; i < 10; i++) {
    int nomeSorteado = gerador.nextInt(nomes.size() - 1);
    nomesSorteados[i] = nomes[nomeSorteado];
}

nomesSorteados[i] = nomes[nomeSorteado]; 的部分是错误所在的行。感谢您的帮助。

【问题讨论】:

  • 你不能用数组索引语法访问List的元素,你需要调用list.get(index)或者list.set(index, value)或者list.add(value)等等。

标签: java class methods parameters


【解决方案1】:

看起来您的 nomesSorteadosnomes 具有 List 类型。您不能使用 [index] 下标索引列表,但您可以在其上调用 get/add 方法。

for(int i = 0; i < 10; i++) {
    int nomeSorteado = gerador.nextInt(nomes.size() - 1);
    nomesSorteados.add(nomes.get(nomeSorteado));
}

【讨论】:

    猜你喜欢
    • 2013-03-08
    • 1970-01-01
    • 2021-07-24
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    相关资源
    最近更新 更多