Results 1 to 9 of 9
Thread: Help with arraylist
- 09-20-2010, 07:53 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Help with arraylist
Hi all
I'm new to java programming, and I really would appreciate some of your help.
Let say I have two classes, one is called Person, in this class I set things like name, adress, age and so on...My other class is Bike, this class is used for setting size of the bike, color, kind of bike and so on...
My issue is that I want to collect all of this in an arraylist, but really need your help on how to do that.
For example I can write Person person = new Person(name, age, ...) and also write Bike bike = new Bike(color, size, ...), one person can own one or many bikes. Let say I create two persons, (A & B) and three bikes (1 & 2 & 3) and I want to collect this in one (if necassary more than one) arraylist. I need to relate Mr. A to bike 1 & 2 and Mr. B to bike 3. I want to be able to add person and bikes and also to make a list of all persons and their bikes, so that I can see which bike and person are attached to each other. If I choose Mr. A it shows me A and then bike 1 & 2 as an example...
I'm doing this training in console environment.
Could someone please give me a sample code of how to do this, I really want to learn. It would be great if you made some smaller comments, so that I unnderstand what is happening. I have searched this on google for days without any luck. Thank you in advance.
- 09-20-2010, 08:00 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
There is a 'has a' relationship between a Person and Bikes: a Person has a Bike. So add a collection of Bikes to a Person class and implement set/get methods for it in the Person class. Possibly the collection can be a Set<Bike> because a Person only owns (has) a bike only once ...
kind regards,
Jos
- 09-20-2010, 10:26 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Hi thanks for your reply, but please consider that I'm quite new to this so I would really appreciate a code example to make me understand what's going on. I'm not asking you for writing a book just a couple of lines of code to get me started. For example in my bike class should I have "public list myList()" to collect and pass on to person class? Thank you.
-
We usually find that folks learn much better if they try on their own first, and post their code attempts, mistakes and all, if there are problems. Besides, you have nothing to lose!
Best of luck!
- 09-21-2010, 10:54 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Hi again
I found an example and I think I'm on right track now, could you please have a look and comment my code. I can not seem to get the printing correct, it just print the last record and not both if I choose to enter two bikes or more, why? Also any suggestion to how I'm gonna be able to select a person to add another bike later on?
Here is my code:
For my person class...
And for my bike class....Java Code:import java.util.List; public class Person { private List<Bike> bikes; private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Bike> getBikes() { return bikes; } public void setBikes(List<Bike> bikes) { this.bikes = bikes; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return name; } }
And to run the program...Java Code:public class Bike { private String type; private String color; private int year; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String toString() { return type; } public void setYear(int year) { this.year = year; } public int getYear() { return year; } }
Java Code:import java.util.*; public class Handler { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.print("How many reg: "); int quantity = scan.nextInt(); int i = 0; while(i < quantity) { Driver drive = new Driver(); drive.Drive(); i++; } } } class Driver { private String bikeType, ownerName, bikeColor; int year, age; public void Drive() { Scanner scan = new Scanner(System.in); System.out.print("Enter type of bike: "); bikeType = scan.nextLine(); System.out.print("Enter color of bike: "); bikeColor = scan.nextLine(); System.out.print("Enter year of manufacturing: "); year = scan.nextInt(); scan.nextLine(); System.out.print("Enter name of owner: "); ownerName = scan.nextLine(); System.out.print("Enter age of owner: "); age = scan.nextInt(); scan.nextLine(); Bike bikes = new Bike(); bikes.setType(bikeType); bikes.setColor(bikeColor); bikes.setYear(year); List<Bike> list = new ArrayList<Bike>(); list.add(bikes); Person person = new Person(); person.setName(ownerName); person.setAge(age); person.setBikes(list); for(int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }Last edited by braddy; 09-21-2010 at 11:02 PM. Reason: Didn't have code blocks...
- 09-21-2010, 10:58 PM #6
See this: Java Forums - BB Code List
- 09-21-2010, 11:03 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Thanks Norm
-
I think you're better off if Person did not have a setBikes method. Rather, create the ArrayList<Bike> once in the Person constructor, and give Person an addBike(Bike bike) method that adds a Bike to the ArrayList. Also, in your main you should create your one Person before the while loop, and then add Bikes to the person in the while loop.
- 09-22-2010, 06:29 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Creating an ArrayList from an ArrayList
By Klahking in forum New To JavaReplies: 17Last Post: 09-09-2010, 03:34 PM -
Help with arraylist
By alexisasoxfan in forum New To JavaReplies: 12Last Post: 05-07-2010, 06:07 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By kizilbas1 in forum New To JavaReplies: 11Last Post: 12-05-2007, 07:30 PM -
New to arraylist
By kleave in forum New To JavaReplies: 2Last Post: 11-19-2007, 06:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks