Ortiga
Ortiga

Reputation: 8814

Iterating ArrayList inside a method

I have a Candidades class that holds Candidate objects, as follow:

import java.util.*;
public class Candidates<Candidate> extends ArrayList<Candidate>  {

public int getTotalVotesCount()
{
    Iterator it = this.iterator();
    int i, total = 0;

    while(it.hasNext())
    {
        Candidate c = (Candidate)it.next();

        total += c.getVoteCount();
    }
    return total;
}
}

Class Candidate is as follows:

public class Candidate {

private int votes;
private String name;

public String getName()
{
    return this.name;
}

public int getVoteCount()
{
    return this.votes;
}

public void vote()
{
    votes++;
}

public Candidate(String _name)
{
    this.name = _name;
    this.votes = 0;
}
}

How do i iterate over it?

I know the code for the iteration is ok, as using the code outside the class works.

The test is bellow:

/**

 * @(#)Test.java
 *
 * Test application
 *
 * @author
 * @version 1.00 2011/3/8
 */
import java.util.*;
public class Test {
public static void main(String[] args) {

    Candidates candidates = new Candidates();

    candidates.add(new Candidate("One"));
    candidates.add(new Candidate("Two"));
    candidates.add(new Candidate("Three"));
    candidates.add(new Candidate("Four"));

    Iterator it = candidates.iterator();

    int i = 0;
    while(it.hasNext())
    {
        i++;

        Candidate c = (Candidate)it.next();

        for(int j = 0; j <= i; j++)
        {
            c.vote();
        }
    }

    int total = 0;
    it = candidates.iterator();
    while(it.hasNext())
    {
        Candidate c = (Candidate)it.next();
        total += c.getVoteCount();
    }

    System.out.printf("Votes: %d", total);
}
}

The code above correctly prints 14.

Upvotes: 2

Views: 1447

Answers (6)

Fred Haslam
Fred Haslam

Reputation: 9033

If you are trying to iterate over a class from within the class, then use this:

for (Candidate c : this ) ...

Upvotes: 4

Puce
Puce

Reputation: 38122

public class Candidates<Candidate> extends ArrayList<Candidate>  {

This is a list with a type parameter name = Candidate (it's just a name and has nothing to do with the Candidate class)

public class Candidates extends ArrayList<Candidate>  {

This is a list of candidates.

I didn't read the complete problem and all the answers, but extending ArrayList is most likly not what you want to do. More likely you want to use composition rather than inheritance.

Upvotes: 0

bluefoot
bluefoot

Reputation: 10580

There's no need to extend ArrayList (unless you think this may be more legible, or something else you didn't post about).

You can create an ArrayList of Candidates and use foreach to iterate:

List<Candidate> candidates = new ArrayList<Candidate>();
candidates.add(new Candidate("One"));
candidates.add(new Candidate("Two"));
candidates.add(new Candidate("Three"));
candidates.add(new Candidate("Four"));

int total = 0;

foreach(Candidate c : candidates) {
    c.vote();
    total += c.getVoteCount();
}

System.out.printf("Votes: %d", total);

Upvotes: 2

Carl
Carl

Reputation: 7544

Candidates isn't really a subtype of ArrayList - it's not a specialized generic container that extends the capabilities of ArrayList, it's just an ArrayList + convenience method for the specific type being stuck in there.

What I might do for this: Candidate class as you had, Candidates class a static helper class for convenient API:

public final class Candidates {
 private Candidates() {} //singleton enforcer
 public static int getTotalVotes(Iterable<Candidate> candidates) {
  //check for nulls
  int total = 0;
  for (Candidate c : candidates) total += c.getVoteCount();
  return total;
 }
 //other convenience methods
}

then, as others point out, use your collection of choice, and work with the code like:

Collection<Candidate> candidates = new //...whatever
// voting scheme
int totalvotes = Candidates.getTotalVotes(candidates);

Upvotes: 0

Adam
Adam

Reputation: 5070

Don't extend ArrayList, implement List, and use delegation, add your own methods. Also use for-each if you can.

Upvotes: 0

blong824
blong824

Reputation: 4030

I would make my Candidates class like this:

public class Candidates() {
  private List<Candidate> candidates = new ArrayList<Candidate>();

  public int getTotalVotesCount() {
   int total = 0;
   for (Candidate candidate : candidates) {
     total += candidate.getVoteCount();
   }
   return total;
  }
}

You still need to populate candidates but I would recommened using the foreach loop.

Upvotes: 1

Related Questions