Creating multiple objects in Java

SJP0tato

Senior member
Aug 19, 2004
267
0
76
Hi guys,

I'm hoping someone has a good exmaple, or could explain a good way to do what I am attempting.

Basically an example of what I am trying to do is read a list of users from a text file, and create say a "customer" object for each user's name from the text file. I'm somewhat familiar with java, and have everything working except the multiple object creation.

Every example I have seen of object creation involves a single object created from main, for example:
Dog fido = new Dog(labrador, 100lbs, ect);
The program would parse the text file, and substitute the user's name for "fido" in the previous example creating a new customer object for each read customer from the text file.


If this is not possible, what would be a better way to have customers input from an externally modifyable text file that people could enter new users into with various attributes?

If anyone has any examples, or can suggest method that would work I would be in your debt!

Thanks!
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You would have to use some sort of Collection object to store all of them. I would recommend an ArrayList for such a simple program.

ArrayList <Dog> myDogs = new ArrayList<Dog>();

while (reading from text file) {
myDogs.add(new Dog("fido","100lbs",...));
}

That will put all the ones from the text file into teh ArrayList.

There are two ways to get data back from the ArrayList.. you can use an iterator or you can use a for loop ie.
Dog temp;
for (int i = 0; i < myDogs.size(); i++) {
temp = myDogs.get(i);
<do whatever you want with temp>
}

I would recommend reading up on Java Collections and Java iterators... there are many many other types of collections you could use.. like a Map, Set, Hash and many of them are interchangable too.
 

SJP0tato

Senior member
Aug 19, 2004
267
0
76
Aha that's perfect, thanks Crusty.

So each new dog object in this case would be indexed inside of the arraylist, from there the attributes such as name, birthdate, ect could be added/modified as any normal object within the confines of the arraylist.

This makes a lot more sense, much appreciation!
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Yes, in Crusty's example, once you do this:
temp = myDogs.get(i);

You could follow that line with something like:
temp.getBreed();
or temp.weight = 80;

Or whatever the properties of the Dog class are.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Crusty
List <Dog> myDogs = new ArrayList<Dog>();

while (reading from text file) {
myDogs.add(new Dog("fido","100lbs",...));
}

That will put all the ones from the text file into teh ArrayList.
Fixed. No sense in teaching the guy bad habits ;)
... and many of them are interchangable too.
!!
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: kamper
Originally posted by: Crusty
List <Dog> myDogs = new ArrayList<Dog>();

while (reading from text file) {
myDogs.add(new Dog("fido","100lbs",...));
}

That will put all the ones from the text file into teh ArrayList.
Fixed. No sense in teaching the guy bad habits ;)
... and many of them are interchangable too.
!!


How is that a bad habit? There is absolutely nothing wrong with using ArrayList as the type, especially for something as simple as he described.

Introducing ArrayLists is one thing... but introducing inheritance as well is quite overwhelming. Now that you have intoduced inheritance you should epxlain how/why it works as well or you are just going to confuse him.
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
Remember to type cast back to your object if you don?t use <> in creating your array list.you can use the method instanceof to check the type of the dynamic object.

Classes in java are very easy wait untill you see C++ and multiple inheritance and the dimond on doom problem

.......[base class]
.............. /.........\.. ...
[class a] .........[class b]
...............\.........../
.............[class C]

 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Crusty
How is that a bad habit? There is absolutely nothing wrong with using ArrayList as the type, especially for something as simple as he described.

Introducing ArrayLists is one thing... but introducing inheritance as well is quite overwhelming. Now that you have intoduced inheritance you should epxlain how/why it works as well or you are just going to confuse him.
Granted, for something this small it doesn't really make a difference and your point about minimizing confusion is valid.

But in general, programming to an interface that offers almost everything that the class offers (I count 3 methods in ArrayList that can't be called on a List and I've never called any of them) is hugely preferable. It's very frustrating to have seperate chunks of code using Vectors and ArrayLists explicitly and then having to do a big copy over just to exchange data. I came across it once and, instead of hacking, read through all the Vector-using code to make sure that it didn't call anything specific and then changed it to using a List.

If you're using specific functionality of a Vector or ArrayList, that's another thing, but List and even Collection are pretty sufficient for the vast majority of cases.