Connecting Tech Pros Worldwide Forums | Help | Site Map

Learning Java Help please!

Newbie
 
Join Date: Feb 2009
Posts: 2
#1: Feb 20 '09
Hi im learning java through blue j and im having trouble doing an exercise if anyone can help that would be great.
the question is:

Rewrite the getLot method so it doesn't rely on a lot with a particular number being stored at index (number -1) in the collection.You may assume that lots are always stored in increasing order of their lot number.

The method is written ass :

Expand|Select|Wrap|Line Numbers
  1. public Lot getLot(int lotNumber)
  2.     {
  3.         if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
  4.             // The number seems to be reasonable.
  5.             Lot selectedLot = lots.get(lotNumber - 1);
  6.             // Include a confidence check to be sure we have the
  7.             // right lot.
  8.             if(selectedLot.getNumber() != lotNumber) {
  9.                 System.out.println("Internal error: Lot number " +
  10.                                    selectedLot.getNumber() +
  11.                                    " was returned instead of " +
  12.                                    lotNumber);
  13.                 // Don't return an invalid lot.
  14.                 selectedLot = null;
  15.             }
  16.             return selectedLot;
  17.         }
  18.         else {
  19.             System.out.println("Lot number: " + lotNumber +
  20.                                " does not exist.");
  21.             return null;
  22.         }
  23.  
  24.  
Any help would be much appreciated again

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Feb 20 '09

re: Learning Java Help please!


Would a Map<Integer, Lot> be of help? The Integer is the lot number, stored as the key in the Map, the associated value is the Lot itself.

kind regards,

Jos
Newbie
 
Join Date: Feb 2009
Posts: 2
#3: Feb 20 '09

re: Learning Java Help please!


maybe but i havent come across maps yet...im just a wee beginner ha :)

thanks for the help though bud.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Feb 20 '09

re: Learning Java Help please!


Quote:

Originally Posted by Speckydodge View Post

maybe but i havent come across maps yet...im just a wee beginner ha :)

thanks for the help though bud.

Well, then you're stuck with your arrays then. The index number of the array has a functional dependency on the Lot number. There's no way to get rid of it.

kind regards,

Jos
Reply