How to automatically assign a large amount of objects into a list
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
For example, to achieve what you are going for you could do something like this
Tim Driven Development | Test until the fear goes away
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
So the constructor argument isn't just multiples of 10 up to 10000? If not, where does the required value come from?
Tim Driven Development | Test until the fear goes away
remi lionio
Greenhorn
Posts: 5
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It's something like this, though I plan to add more details once I figured out things
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
Is there a relationship between Cars and Elements that I'm missing?
Tim Driven Development | Test until the fear goes away
remi lionio
Greenhorn
Posts: 5
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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,
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
Tim Driven Development | Test until the fear goes away
remi lionio
Greenhorn
Posts: 5
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I see, first time I've heard of it. I'll look at it soon, Thank you.
posted 3 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Tim Driven Development | Test until the fear goes away
| 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 |












