2

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.

3
  • 2
    Why did you extend ArrayList for that? Commented Mar 8, 2011 at 17:35
  • what exactly is the problem? you can iterate over candidates as your main shows. Commented Mar 8, 2011 at 17:41
  • Because this is possible in C# (iterate within it own list), i'd like to know if it would be possible in Java Commented Mar 8, 2011 at 17:48

6 Answers 6

4

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

for (Candidate c : this ) ...
Sign up to request clarification or add additional context in comments.

1 Comment

Clear and concise.
2

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);

1 Comment

That was just an example (I'm learning java, coming from C#), and there will be more methods...
1

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.

1 Comment

(im always a few minutes late to the party ;) )
0

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

1 Comment

Thanks, I'm still testing but I think it does the job
0

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);

Comments

0
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.