• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Creating multiple objects in Java

SJP0tato

Senior member
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!
 
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.
 
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!
 
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.
 
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.
!!
 
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.
 
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]

 
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.
 
Back
Top