ArrayList problems

I'm new to Java, and I'm having trouble with ArrayLists. I'm working through the exercised in a book called "Objects First with Java". I have an array of type 'Lots' called 'lots'. I want to run go through the array, and add any unsold lots to a new array, then return that array. In another method, I want to loop over the array returned from getUnsold() and print the list. The problem is, the lost is being added to the array as an Object, not a Lot, so I get an error that a 'Lot' was expected but an 'Object' was returned. Am I missing something blindingly obvious here?

public ArrayList getUnsold() {

Iterator it = lots.iterator();

Lot templot;

ArrayList lots;

while(it.hasNext()) {

templot = it.next();

if(templot.getHighestBid() == null) {

unsold.add(templot);

}

}

return unsold;

}

public void showUnsold() {

ArrayList unsold;

unsold = getUnsold();

for(Lot unsoldlot : unsold) {

unsoldlot.toString();

}

}

}

[1052 byte] By [Echilona] at [2007-11-26 17:20:36]
# 1
yep. you need to use [url= http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html]generics[/url]
georgemca at 2007-7-8 23:48:36 > top of Java-index,Java Essentials,New To Java...
# 2
> yep. you need to use genericsOr if you're not using 1.5+, you can just cast it.templot = (Lot)it.next();
CaptainMorgan08a at 2007-7-8 23:48:36 > top of Java-index,Java Essentials,New To Java...
# 3

Or loop through the Lots array directly

public void showUnsold()

{

for (Lot lot : lots)

if (isUnsold(lot))

System.out.println(lot); // toString() called implicitly if it is defined in Lot

}

where function isUnsold(Lot) checks if the lot is infact unsold.

Cheers

duckbilla at 2007-7-8 23:48:36 > top of Java-index,Java Essentials,New To Java...
# 4
> Or loop through the Lots array directlyBut, again, this was new to 1.5.0.
CaptainMorgan08a at 2007-7-8 23:48:36 > top of Java-index,Java Essentials,New To Java...
# 5
and if you look at the OPs code, he is indeed using enhanced for loops, ergo, java 1.5+
georgemca at 2007-7-8 23:48:36 > top of Java-index,Java Essentials,New To Java...
# 6

> > Or loop through the Lots array directly

>

> But, again, this was new to 1.5.0.

Yes, so if you're using version < 1.5, use

for (int i = 0; i < lots.length; i++)

if (isUnsold(lots[i]))

// as above

duckbilla at 2007-7-8 23:48:36 > top of Java-index,Java Essentials,New To Java...
# 7
> and if you look at the OPs code, he is indeed using> enhanced for loops, ergo, java 1.5+So he is! Ignore me, please.
CaptainMorgan08a at 2007-7-8 23:48:36 > top of Java-index,Java Essentials,New To Java...
# 8

Echy, Which version of java are you on 1.5

... as you've heard allready the later versions of Java make like easier for you...

type javac -version at the command prompt to find out... and if that doesn't work try java -version... and if that doesn't work you version of java is too old, [url=http://java.sun.com/javase/downloads/index.jsp]download the 1.6 JDK[/url] from Sun.

Keith.

corlettka at 2007-7-8 23:48:36 > top of Java-index,Java Essentials,New To Java...
# 9

> Echy, Which version of java are you on 1.5

>

> ... as you've heard allready the later versions of

> Java make like easier for you...

>

> type javac -version at the command prompt to find

> out... and if that doesn't work try java -version...

> and if that doesn't work you version of java is too

> old,

> [url=http://java.sun.com/javase/downloads/index.jsp]do

> wnload the 1.6 JDK[/url] from Sun.

>

> Keith.

see reply #5

georgemca at 2007-7-8 23:48:37 > top of Java-index,Java Essentials,New To Java...
# 10

The problem's half solved. I've decided to use a for loop instead, but now I'm getting a compile error that the variable 'unsold' might not have been initialised

public ArrayList getUnsold() {

ArrayList<Lot> unsold;

for(Lot templot : lots) {

if(templot.getHighestBid() == null) {

unsold.add(templot);// Error: the unsold variable might not have been initialised

}

}

return unsold;

}

Echilona at 2007-7-8 23:48:37 > top of Java-index,Java Essentials,New To Java...
# 11

You left unsold unitialized. Also, you should make your return type List<Lot>, not just List or just ArrayList:

public List<Lot> getUnsold() {

List<Lot> unsold = new ArrayList<Lot>();

for(Lot templot : lots) {

if(templot.getHighestBid() == null) {

unsold.add(templot);

}

}

return unsold;

}

DrLaszloJamfa at 2007-7-8 23:48:37 > top of Java-index,Java Essentials,New To Java...
# 12
Thanks alot, it's much clearer now :)
Echilona at 2007-7-8 23:48:37 > top of Java-index,Java Essentials,New To Java...