【问题标题】:Array (list?) required, but int found error需要数组(列表?),但 int 发现错误
【发布时间】:2015-10-11 04:35:39
【问题描述】:

我有以下代码:

import java.util.ArrayList;
public class TestCandidate2
{
public static void main(String[] args) {
 ArrayList<Candidate> election = new ArrayList<Candidate>();

 Candidate john = new Candidate("John Smith", 5000);
 election.add(john);
 Candidate mary = new Candidate("Mary Miller", 4000);
 election.add(mary);
 Candidate michael = new Candidate("Micheal Duffy", 6000);
 election.add(michael);
 Candidate tim = new Candidate("Tim Robinson", 2500);
 election.add(tim);
 Candidate joe = new Candidate("Joe Ashtony", 1800);
 election.add(joe);

 System.out.println("Results Per Candidate:");
 System.out.println("________________________");
 System.out.print("\n");
 int totalVotes = 0;
 int total = 0;

 for(Candidate dec : election) {
    System.out.println(dec.toString());
    total += dec.getVotes();
    totalVotes += dec.getVotes();
 }

 System.out.print("\n");
 System.out.println("Total number of votes in election: " + totalVotes);
}

public static void printVotes(ArrayList<Candidate> table) {
    for(int i = 0; i < table.size(); i++) {
       System.out.println(table.size()[i]);
    }
}

/**public static int getTotal(ArrayList<Candidate> table)  {
    int total = 0;
    for(int i = 0; i < table.size(); i++) {
       total = table[i].getVotes() + total;
    }
    return total;
}*/

/**public static void printResults(ArrayList<Candidate> table)  {
    double total = getTotal(table);
    System.out.print("Candidate               Votes Received              % of Total Votes");
    System.out.print("\n");
    for(int i = 0; i < table.length; i++) {
       System.out.printf("%s %17d %25.0f", table[i].getName(), table[i].getVotes(), ((table[i].getVotes() / total) * 100));
       System.out.println("");
    }
}*/
}

首先,我认为我应该得到的错误是“需要数组列表,但找到了 int”,但我得到的错误是“需要数组,但找到了 int”。

我不知道我应该如何修复 int i,因为每当我将 [] 声明为数组时,我仍然会出错。我试过研究,但似乎没有人有我的确切问题。

【问题讨论】:

  • 哪行代码导致错误?
  • System.out.println(table.size()[i]);

标签: java arrays


【解决方案1】:

您似乎错误地访问了您的Lists。而不是:

table[i].getVotes()

尝试:

table.get(i).getVotes();

或者完全避免使用索引。例如,您可以将getTotal() 重写为:

public static int getTotal(List<Candidate> candidates) {
    int total = 0;
    foreach (Candidate c : candidates) {
        total += c.getVotes();
    }
    return total;
} 

产生错误的行:

System.out.println(table.size()[i]);

有点混乱,但我认为你想要:

System.out.println(table.get(i).getVotes());

【讨论】:

  • 非常感谢,这对我有用,我现在明白为什么以前没有。
  • 没问题。如果您对答案感到满意,您可以接受它作为正确答案。
【解决方案2】:

您可以使用List.get(int) 按位置获取元素(不是[] 用于数组)。另外,我建议你编程到列表界面。喜欢

public static void printVotes(List<Candidate> table) {
    for(int i = 0; i < table.size(); i++) {
       System.out.println(table.get(i));
    }
}

您也可以使用for-each loop+= 之类的,

public static int getTotal(List<Candidate> table)  {
    int total = 0;
    for(Candidate c : table) {
       total += c.getVotes();
    }
    return total;
}

【讨论】:

    【解决方案3】:

    您应该使用table.get(i) 而不是table[i]。这是ArrayList 不是数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多