• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

How to automatically assign a large amount of objects into a list

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say I have a custom class named Car, and I have a 1000 instance of this class, it will be troublesome to do like list.add(c1); list.add(c2) so what I'm asking is how do you do this in java, I have some idea but I don't know if it works

 
Marshal
Posts: 6212
502
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're on the right track with using a loop. When you want to create n instances of a type then create a loop that iterates n times and put the object creation inside it.

For example, to achieve what you are going for you could do something like this
 
Marshal
Posts: 28492
113
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Remi!



That is how you do it in Java. You don't need to assign the object to a variable before you add it to the list. And even if you did assign it to a variable, the name of that variable is irrelevant. E.g.

 
remi lionio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep I understand all that, but I only said Car object because it's simpler, but what I'm does is the table of elements so I can't really put (i*10). Are there some ways to do this? Or should I just manually add them in a list?
 
Tim Cooke
Marshal
Posts: 6212
502
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the constructor argument isn't just multiples of 10 up to 10000? If not, where does the required value come from?
 
remi lionio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's something like this, though I plan to add more details once I figured out things

 
Tim Cooke
Marshal
Posts: 6212
502
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused. That appears to bear no relation to your original question.

Is there a relationship between Cars and Elements that I'm missing?
 
remi lionio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I apologize, what I really want to focus on the question is not the parameters but how to convert a string into an instance of the object,
 
Tim Cooke
Marshal
Posts: 6212
502
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah I see. In that case the answer is no, but yes.

There is nothing in the Java language that will directly do what you need in the way you describe in your first post. But there is a design pattern that you may find useful, the "Static Factory Method". You will find examples of it all over the JDK, for example Integer.valueOf(String val) and Calendar.getInstance().

In your example you'll want to write something that behaves like this
or

Then it's up to you to define the format of the given String and it's relationship to the object fields.
 
remi lionio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, first time I've heard of it. I'll look at it soon, Thank you.
 
Tim Cooke
Marshal
Posts: 6212
502
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example of how you might apply it to your Car class. I am converting the loop index to a String when I clearly don't have to but am doing so to demonstrate how you can write a static factory method to take a String and create a Car for you.

 
This tiny ad will self destruct in five seconds.
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic