在 Java 中打印 arrayList 的值

Print values of arrayList in Java

我现在遇到的三个错误来自以下行:

printResults(election);

我该如何解决它才能打印出选票的总价值?谢谢。

TessCandidate4

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

 public static int getTotal(List<Candidate> election)
{
  int total = 0;
  for(Candidate candidate : election )
  {
    total += candidate.numVotes;
  }
   return total;
}

 public static void printResults(Candidate[] election)
{
  double percent;
  System.out.println("Candidate        Votes Received      % of Total Votes");

 for (Candidate candidate : election)
{
  percent = (double) (candidate.votes()) / getTotal(election) * 100;
  System.out.printf("%-15s %10d %20.0f", candidate.getName(), candidate.votes(), percent);
  System.out.println();
}

}

  public static void replaceName(List<Candidate> election, 
                                String find, String replace) 
 {
   for(int index = 0; index < election.size(); index++)
      if (election.get(index).getName().equals(find))
           election.get(index).setName(replace);
 }

  public static void replaceVotes(List<Candidate> election, 
                                String find, int replace) 
 {
   for(int index = 0; index < election.size(); index++)
      if (election.get(index).getName().equals(find))
           election.get(index).setVotes(replace);
 }

    public static void replaceCandidate(List<Candidate> election, 
                                String find, String replace, int replaceV) 
 {
   for(int index = 0; index < election.size(); index++)
      if (election.get(index).getName().equals(find))
   {
     election.get(index).setName(replace);
           election.get(index).setVotes(replaceV); 
   }
 }


 public static void main(String[] args)
 {
    List<Candidate> election = new ArrayList<Candidate>();

    // create election
    election.add(new Candidate("John Smith", 5000));
    election.add(new Candidate("Mary Miller", 4000));        
    election.add(new Candidate("Michael Duffy", 6000));
    election.add(new Candidate("Tim Robinson", 2500));
    election.add(new Candidate("Joe Ashtony", 1800));  
    election.add(new Candidate("Mickey Jones", 3000));
    election.add(new Candidate("Rebecca Morgan", 2000));
    election.add(new Candidate("Kathleen Turner", 8000));
    election.add(new Candidate("Tory Parker", 500));
    election.add(new Candidate("Ashton Davis", 10000));

    System.out.println("Original results:");
    System.out.println();
    System.out.println(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );
    System.out.println();

    replaceName(election, "Michael Duffy", "John Elmos");
    System.out.println("Changing Michael Duffy to John Elmos:");
    System.out.println();
    System.out.println(election);
    System.out.println("Total of votes in election: " + getTotal(election) );   
    System.out.println();

    replaceVotes(election, "Mickey Jones", 2500);
    System.out.println("Changing Mickey Jones to 2500:");
    System.out.println();
    printResults(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );   
    System.out.println();

    replaceCandidate(election, "Kathleen Turner", "John Kennedy", 8500);
    System.out.println("Changing Mickey Jones to 2500");
    System.out.println();
    printResults(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );
    System.out.println();

   }

}

候选人

public class Candidate
{
 // instance variables 
  int numVotes;
  String name;

/**
  * Constructor for objects of class InventoryItem
  */
  public Candidate(String n, int v)
  {
   // initialise instance variables
   name = n;
   numVotes = v;
  }
  public int votes() 
  {
    return numVotes;
  }
  public void setVotes(int num)
  {
    numVotes = num;
  }
  public String getName()
  {
    return name;
  }
  public void setName(String n)
  {
    name = n;
  }    
  public String toString()
  {
    return name + " received " + numVotes + " votes.";
 }

}

您使用的是数组,而不是 ArrayList。他们是不同的。

您可以在此处了解 ArrayList 以及如何使用它们:http://www.homeandlearn.co.uk/java/array_lists.html

要创建候选人的 ArrayList,您将使用以下语法:

ArrayList<Candidate> array_name = new ArrayList<Candidate>()

要将 ArrayList 作为参数传递,您需要执行类似的操作:

public void method_name(ArrayList<Candidate> argument_name) { ... }

关于arrays和arraylist的清晰解释,请阅读以下内容post:How to add new elements to an array?

我认为您还需要按以下方式(或类似方式)重写您的 printResults:

public static void printResults(Candidate[] election)
 {
   double percent;
   int getTotalVotes = getTotal(election); //To calculate this once, it saves you execution time, with large numbers

   System.out.println("Candidate        Votes Received      % of Total Votes");

   for (Candidate candidate : election)
   {
      percent = (double) (candidate.votes()) / getTotalVotes * 100;
      System.out.printf("%-15s %10d %20.0f", candidate.getName(), candidate.votes(), percent);
      System.out.println();
   }
 }

因为election是一个数组, 要获取第 x 个元素,请使用 election[x]。 如果它是 List,您将使用 election.get(x)

因此您要求更正的行应该是:

percent = (double) (election[x].votes()) / getTotal(election) * 100;

其后一行:

System.out.printf("%-15s %10d %20.0f", election[x].getName(), election[x].votes(), percent);

但是你的代码仍然不能工作, 因为在 main 方法中,您在 List 中添加候选项, 但是所有方法都需要一个数组而不是 List。 一个快速的解决方法是像这样更改 main 方法的开头:

List<Candidate> electionList = new ArrayList<Candidate>();

// create election
electionList.add(new Candidate("John Smith", 5000));
electionList.add(new Candidate("Mary Miller", 4000));
electionList.add(new Candidate("Michael Duffy", 6000));
electionList.add(new Candidate("Tim Robinson", 2500));
electionList.add(new Candidate("Joe Ashtony", 1800));
electionList.add(new Candidate("Mickey Jones", 3000));
electionList.add(new Candidate("Rebecca Morgan", 2000));
electionList.add(new Candidate("Kathleen Turner", 8000));
electionList.add(new Candidate("Tory Parker", 500));
electionList.add(new Candidate("Ashton Davis", 10000));

Candidate[] election = electionList.toArray(new Candidate[electionList.size()]);

即填充一个 List,然后将其转换为数组。

更好的解决方案是在所有地方使用 List。 例如改变这个方法:

public static void replaceVotes(Candidate[] election, String find, int replace) {
    for (int index = 0; index < election.length; index++)
        if (election[index].getName().equals(find))
            election[index].setVotes(replace);
}

这样重写:

public static void replaceVotes(List<Candidate> election, String find, int replace) {
    for (Candidate candidate : election) {
        if (candidate.getName().equals(find)) {
            candidate.setVotes(replace);
        }
    }
}