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("Tw开发者_如何学运维o"));
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.
If you are trying to iterate over a class from within the class, then use this:
for (Candidate c : this ) ...
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 Candidate
s 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);
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.
Don't extend ArrayList
, implement List
, and use delegation, add your own methods. Also use for-each if you can.
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);
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.
精彩评论