I am trying to write public instance method createParther()
that returns an instance of Couple
. The method does its job by following these rules: one dancer in the couple must be from aList
and the other bList
, one dancer in the couple must be male and the other female. Neither dancer in the couple should be partnered already. If it is not possible to create a couple from amongst the unpartnered dancers, then null
should be returned. If an instance of Couple
is successfully created, both dancers involved in the couple should have their partnered instance variables set to true
. I have attempted to list all the aList
and bList
to together, but then I didn't know to check the requirements as per above. Can anyone demostrate how this could be acheieved? This is not assignment.
public class FoxDancing
{
private List<Couple> coupleList;
private List<Dancer> aList;
private List<Dancer> bList;
public FoxDancing()
{
couplesList = new ArrayList<Parth开发者_Go百科ers>();
aList = new ArrayList<Dancer>();
bList = new ArrayList<Dancer>();
}
public void fieldLists()
{
this.addX("Simon","AList",'m');
this.addX("Jason","AList",'m');
this.addX("Ian","AList",'m');
this.addX("Susan","BList",'f');
this.addX("Helena","BList",'f');
this.addX("Gina","BList",'f');
}
}
It looks like you'd want to have a helper method like this:
Dancer findUnpartnered(List<Dancer> list) {
for (Dancer d : list) {
if (d.isUnpartnered()) {
return d;
}
}
return null;
}
This uses a "foreach loop" for a concise, readable iteration over all Dancer
in the List<Dancer>
.
Then you can write something like this:
Couple createCouple() {
Dancer a = findUnpartnered(aList);
Dancer b = findUnpartnered(bList);
if (a == null || b == null) {
return null;
} else {
a.setPartnered(true);
b.setPartnered(true);
return new Couple(a, b);
}
}
While this should work, note that findUnpartnered
is a O(N)
linear search. If the list is of any considerable length, consider having alternative data structures, e.g. a Set<Dancer>
that partitions the list into unpartnered and partnered subsets.
On generics invariance
You've written the following:
// snippet from original code
private List<Couple> coupleList;
//...
couplesList = new ArrayList<Parthers>(); // DOES NOT COMPILE!
This will not compile. A List<Partner>
(assuming this is what you meant to write) is NOT a List<Couple>
. Perhaps you want a new ArrayList<Couple>
, or if Partner
is a subtype of Couple
, then perhaps you want a List<? extends Couple> coupleList
.
Related questions
- What is the difference between
<E extends Number>
and<Number>
?
See also
- Java Tutorials/Generics and Subtyping and More Fun With Wildcards
- Angelika Langer's Java Generics FAQ - What is a bounded wildcard?
On enum
You've written the following:
// snippet from original code
this.addX("Simon","AList",'m');
this.addX("Susan","BList",'f');
// potentially "bad" use of String and char constants!
I don't know much about Foxtrot dancing, but if there's a conceptual A-list and B-list , then you may consider using an enum
instead of String
markers "AList"
and "BList"
. Similarly, if there can only be male or female dancers, then enum
would be much better than 'm'
and 'f'
.
enum FoxTrotter { A, B; }
enum Sex { MALE, FEMALE; }
See also
- Java Language Guide/Enums
Related questions
- Enumerations: Why? When?
Maybe I don't fully understand your requirements, but since it looks like aList
is made up of males and bList
females, this could be as simple as shuffling both lists and pairing couples up in a loop until one list is empty.
First write dancer class, complete with all the methods you'll need. Then consider if you want to create an instance of Couple or if you just want to have a each dancer have a reference to their partner. Once you get to this point the rest should start falling into place. :D
精彩评论