在Java中打印arrayList的值

时间:2015-02-14 21:46:53

标签: java arraylist

我现在得到的三个错误来自这一行:

printResults(election);

我如何解决这个问题,以便打印出投票的总价值?感谢。

TesstCandidate4

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.";
 }

}

3 个答案:

答案 0 :(得分:0)

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

您可以在此处了解ArrayLists以及如何使用它们: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) { ... }

答案 1 :(得分:0)

有关数组和arraylist的清晰说明,请阅读以下帖子: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();
   }
 }

答案 2 :(得分:0)

由于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);
        }
    }
}