Results 1 to 20 of 21
Thread: Require help with ArrayList
- 08-19-2011, 10:48 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
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.
- 08-19-2011, 11:25 AM #2
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
You could use a hashtable and base the key off the barcode.
- 08-19-2011, 11:28 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
- 08-19-2011, 12:17 PM #4
The Collection Interface (The Java™ Tutorials > Collections > Interfaces)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.
db
- 08-19-2011, 12:22 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 08-19-2011, 01:05 PM #6
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 .
- 08-19-2011, 01:30 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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).
- 08-19-2011, 03:15 PM #8
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
Let's make this clear:
This code:
Is not in any way using this class:Java Code:ArrayList<Item> ItemList = new ArrayList<Item>(); ItemList.add(new Grocery(50, "APPLE", 1, 0.0, 2.50)); ...
Given that, DarrylBurke's link above should prove helpful.Java Code:public class ItemList { ...
- 08-19-2011, 04:24 PM #9
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
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.
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.Java Code:public interface Iterator<E> { boolean hasNext(); E next(); void remove(); //optional }
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.
- 08-19-2011, 04:37 PM #10
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: []
- 08-19-2011, 04:44 PM #11
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
It would look like this:
or maybe like this:Java Code:Iterator<Item> iterator = ItemList.iterator(); while (iterator.hasNext()) { Item anItem = iterator.next(); System.out.println(anItem); }
Java Code:for (Item anItem : ItemList) { System.out.println(anItem); }
- 08-19-2011, 04:49 PM #12
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
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
- 08-19-2011, 04:49 PM #13
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
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.)
- 08-19-2011, 04:52 PM #14
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
- 08-19-2011, 04:52 PM #15
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
Try this:
or this:Java Code:import java.util.Iterator;
Java Code:import java.util.*;
- 08-19-2011, 04:55 PM #16
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
- 08-19-2011, 04:59 PM #17
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
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 :\
- 08-19-2011, 05:04 PM #18
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 @.Toys@e231b4
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.
- 08-19-2011, 05:07 PM #19
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
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.
)
- 08-19-2011, 05:11 PM #20
Member
- Join Date
- Aug 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Require serious help (scabble like game again sorry...)
By leviathan in forum New To JavaReplies: 1Last Post: 06-04-2010, 08:22 PM -
SCJP Voucher require
By sithes.nmc@gmail.com in forum Java CertificationReplies: 0Last Post: 02-11-2010, 02:10 PM -
Require HELP, Please
By nura23 in forum New To JavaReplies: 5Last Post: 01-10-2010, 02:41 AM -
Require Help with Layout (GridBag)
By hemanthjava in forum AWT / SwingReplies: 2Last Post: 10-22-2008, 01:56 PM -
Require some insight!
By Shaolin in forum JavaServer Pages (JSP) and JSTLReplies: 13Last Post: 07-15-2008, 11:36 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks