Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree3Likes

Thread: Require help with ArrayList

  1. #1
    Mario_1990 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default Require help with ArrayList

    Hi I've started learning Java OOP a couple of weeks back and I've hit a huge wall which I'll explain in detail.

    I've been asked to make a small market program which can do things like modify prices, add products, remove products, etc.

    I've finished all my classes and so forth and I'm now at the point where a user would request information to access certain features on the system. The first function I wanted to add was being able to request a products(objects) bar-code number through a command line interface by simply entering it in.

    I have methods like getBarcode() which would obviously do that but the problem is I have 0 knowledge on HOW I'm actually going to access the array and have it read each section of the array i.e. [0], [1], [2]... until it finally finds that bar code and displays that products information.

    Finally I'll show you some code that I think we be useful.

    ArrayList<Item> ItemList = new ArrayList<Item>();

    ItemList.add(new Grocery(50, "APPLE", 1, 0.0, 2.50));
    ItemList.add(new Grocery(50, "BANANA", 2, 0.0, 2.50));
    ItemList.add(new Grocery(50, "COCONUT", 3, 0.0, 5.00));
    ItemList.add(new Grocery(50, "MANGO", 4, 0.0, 3.75));
    ItemList.add(new Grocery(50, "ORANGE", 5, 0.0, 2.50));
    ItemList.add(new Houseware(25, "LARGE COUCH", 6, 0.0, 250, "COTTON"));
    ItemList.add(new Houseware(25, "LARGE CHAIR", 7, 0.0, 275, "VELVET"));
    ItemList.add(new Sweets(50, "SNICKERS", 8, 0.0, 1.00, "CHOCOLATE"));
    ItemList.add(new Sweets(50, "MALTESERS", 9, 0.0, 1.00, "CHOCOLATE"));
    ItemList.add(new Toys(75, "G.I. JOE", 10, 0.0, 10, "M"));
    ItemList.add(new Toys(75, "BARBIE", 11, 0.0, 10, "F"));

    If anyone could tell me how I can do this it would be incredibly helpful.
    Last edited by Mario_1990; 08-19-2011 at 10:57 AM.

  2. #2
    R-J
    R-J is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    You could use a hashtable and base the key off the barcode.

  3. #3
    Mario_1990 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by R-J View Post
    You could use a hashtable and base the key off the barcode.
    Unfortunately we were instructed to specifically use this type of array. I should have said that earlier, sorry.

    But we were never actually told how to extract key pieces of information which is a little annoying considering the task presented to us.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    I have 0 knowledge on HOW I'm actually going to access the array and have it read each section of the array i.e. [0], [1], [2]... until it finally finds that bar code and displays that products information.
    The Collection Interface (The Java™ Tutorials > Collections > Interfaces)

    db
    JeffGrigg likes this.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    Iterate over the array and compare the entered barcode with the barcode of each entry.
    Presumably you have been given some idea how to iterate over a List?

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    The class ItemList could look like

    Java Code:
    public class ItemList {
    
    	private int counter;
    	private Item[] items ;
    	
    	public ItemList(int size) {
    		items = new Item[size];
    		counter = 0;
    	}
    	
    	public int getSize() {
    		return items.length;
    	}
    	
    	public void add(Item i) {
    		items[counter++] = i;
    	}
    }

    the method add accepts all object that have a relation IS-A to the class Item. For more details read the link posted by DarrylBurke .

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    The OP has already given the sample for using an ArrayList, though, so I suspect they're supposed to (unlike many exercises we see here admittedly) use the Collections framework (or even simply ArrayList).

  8. #8
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Let's make this clear:

    This code:
    Java Code:
    ArrayList<Item> ItemList = new ArrayList<Item>();
    
    ItemList.add(new Grocery(50, "APPLE", 1, 0.0, 2.50));
    ...
    Is not in any way using this class:
    Java Code:
    public class ItemList {
    ...
    Given that, DarrylBurke's link above should prove helpful.

  9. #9
    Mario_1990 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    You guys will probably hate me for this but I'm still confused how to implement this iterator function, I'm just really having a hard time understanding this type of programming style.

    Java Code:
    public interface Iterator<E>
    {
    boolean hasNext();
    E next();
    void remove(); //optional
    }
    I don't mean to come off lazy but if somebody could show me how to run this iterator by the array I've written it would be very helpful as I don't really understand it all that well, specifically how it's suppose to find a specific attribute inside my objects constructor i.e. (Bar-code) which resides in that ArrayList that I've shown you guys.

    It's quite frustrating because I'm generally an alright programmer in my classes I just need this one part shown to me in detail and I'd essentially be able to do every other function in my interface easily.
    Last edited by Mario_1990; 08-19-2011 at 04:27 PM.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    You can always use a for loop like you would use with an array. The difference would be you would use a method to get the elements from the ArrayList vs using the array notation: []
    JeffGrigg likes this.

  11. #11
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    It would look like this:
    Java Code:
    		Iterator<Item> iterator = ItemList.iterator();
    		while (iterator.hasNext()) {
    			Item anItem = iterator.next();
    			System.out.println(anItem);
    		}
    or maybe like this:
    Java Code:
    		for (Item anItem : ItemList) {
    			System.out.println(anItem);
    		}

  12. #12
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    By the way, I don't see a bar code in your data.

    Looking at a few things around here that have bar codes on them, I see...
    • 0 121300 7
    • 8 83585 82973 6
    • 0 72785 10000 8
    • 0 35000 49840 3

  13. #13
    Mario_1990 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    I'm getting this error:

    cannot find symbol
    Iterator<Item> iterator = ItemList.iterator();

    Do I need to import something into Java possibly? Because the page I looked at before didn't specify (or I'm blind and didn't see it.)

  14. #14
    Mario_1990 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by JeffGrigg View Post
    By the way, I don't see a bar code in your data.

    Looking at a few things around here that have bar codes on them, I see...
    • 0 121300 7
    • 8 83585 82973 6
    • 0 72785 10000 8
    • 0 35000 49840 3
    Just for simplicity so that I could test it easier I assigned the third value in my objects constructor as the bar-code, obviously it would have more then a single digit but it wouldn't really matter in terms of testing the program would it?

  15. #15
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Try this:
    Java Code:
    import java.util.Iterator;
    or this:
    Java Code:
    import java.util.*;

  16. #16
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Quote Originally Posted by Mario_1990 View Post
    Just for simplicity so that I could test it easier I assigned the third value in my objects constructor as the bar-code, obviously it would have more then a single digit but it wouldn't really matter in terms of testing the program would it?
    That should work just fine. I mentioned it because I didn't see anything that looked like a bar code. It'll be easier for you to test with smaller numbers, as you are doing.

  17. #17
    Mario_1990 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Awesome that does read through my entire ArrayList but it only seems to display the class from which that object comes from the rest of the information is jumbled with letters and numbers that don't seem to serve any purpose.

    I'll give a small example:

    Grocery@
    Grocery@
    Grocery@
    Grocery@
    Grocery@
    Houseware@
    Houseware@
    Sweets@
    Sweets@
    Toys@
    Toys@

    After the '@' it displays random numbers and letters for some reason :\

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,558
    Rep Power
    25

    Default

    Toys@e231b4
    What you are seeing is the String returned by the Object class's toString method. It adds the memory address of the object after the @.
    If you want to see the contents of your classes, add a toString method to them that returns the String you want to see when you (by default) call their toString method.
    JeffGrigg likes this.

  19. #19
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Quote Originally Posted by Mario_1990 View Post
    Awesome that does read through my entire ArrayList but it only seems to display the class from which that object comes from the rest of the information is jumbled with letters and numbers that don't seem to serve any purpose. ... After the '@' it displays random numbers and letters for some reason :\
    Yep. The idea is that you would replace the 'System.out.println(anItem);' line with code that you write. You'd probably want to use an 'if' statement. Using 'break;' or possibly 'return' would probably be a good idea.

    (Just for your edification: Those funny hex numbers just happen to be the '.hashCode()' values for those instances. You'll probably learn about that later. )

  20. #20
    Mario_1990 is offline Member
    Join Date
    Aug 2011
    Posts
    11
    Rep Power
    0

    Default

    Thanks for all the useful information guys I'll go and try using these ideas for the next 20 - 30 minutes and see where it takes me.

    Fingers crossed!

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 06-04-2010, 08:22 PM
  2. SCJP Voucher require
    By sithes.nmc@gmail.com in forum Java Certification
    Replies: 0
    Last Post: 02-11-2010, 02:10 PM
  3. Require HELP, Please
    By nura23 in forum New To Java
    Replies: 5
    Last Post: 01-10-2010, 02:41 AM
  4. Require Help with Layout (GridBag)
    By hemanthjava in forum AWT / Swing
    Replies: 2
    Last Post: 10-22-2008, 01:56 PM
  5. Require some insight!
    By Shaolin in forum JavaServer Pages (JSP) and JSTL
    Replies: 13
    Last Post: 07-15-2008, 11:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •