Fill a list with objects
Solved
jojo@1989
Posted messages
3
Status
Member
-
jojo@1989 Posted messages 3 Status Member -
jojo@1989 Posted messages 3 Status Member -
Bonjour,
Yes, what you’ve done is mostly correct, but there are a few improvements you can make. Here’s a revised version of your code:
```java
User[] user = new User[nbr];
for(int i = 0; i < nbr; i++){
user[i] = new User();
}
ArrayList<user> list = new ArrayList<>(Arrays.asList(user));
// No need to add the same users again to the list
```
In this version, you don’t need to re-add the users to the list since they’re already included when you create the `ArrayList` from the array. Make sure that the `User` class has been defined correctly with the necessary constructors and methods.
Let me know if you need further assistance!</user>
Yes, what you’ve done is mostly correct, but there are a few improvements you can make. Here’s a revised version of your code:
```java
User[] user = new User[nbr];
for(int i = 0; i < nbr; i++){
user[i] = new User();
}
ArrayList<user> list = new ArrayList<>(Arrays.asList(user));
// No need to add the same users again to the list
```
In this version, you don’t need to re-add the users to the list since they’re already included when you create the `ArrayList` from the array. Make sure that the `User` class has been defined correctly with the necessary constructors and methods.
Let me know if you need further assistance!</user>
3 answers
-
You can definitely tell that you come from C ^^.
We don't use "[]" in Java; in the Javadoc, you already have plenty of containers, such as vectors, lists, etc.
Then, to know if what you did is good, nothing is better than testing.
Finally, I don't understand why you're making an algorithm with complexity O(2n) when you could make one with complexity O(n) ..
Basically, why don't you just do this:ArrayList<User> list = new ArrayList<User>(); for(int i=0;i<nbr;i++){ list.add(new User()); }-
-
-
Yes, I read it well, you said "don't use [] in Java"
Example: the memory consumption of a wrapper list (Character, Integer...) is much greater than the memory consumption of an array of primitive types (char, int...)
Extreme case: the byte is 1 byte, so an array of N bytes is N+4 bytes (4 is the size of the array object), a List<Byte> will at minimum take 5N+4 bytes (5 is the 4 bytes of the Byte object, plus the byte of the primitive byte). Memory loss: factor 5!!!
And I'm not even talking about 64-bit machines, where each object is 8 bytes...
If you don't see them, arrays are everywhere in Java. Example: a String internally is a char[], that's why they are immutable, because arrays are too...
Note along the way: Vector and ArrayList are the same thing except that the former uses a multi-thread security mechanism that will be useless in 99% of cases but will unnecessarily burden your program!
Just because we code in Java doesn't mean we should ignore optimizations, quite the contrary, when you see the monstrosities that can be obtained with very little! -
Mwahahahaha since when do we do embedded programming with a high-level language like Java??
I expected this argument, but in that case tell me:
do you do everything yourself when you code in Java, and do you not use anything that is provided in the Javadoc? (Well yes, you don't use all the functionalities of data structures in general, not to mention function calls, attribute declarations, and methods that take up memory space).
From the moment you use high-level code, you necessarily don't think about optimization, so your argument sounds nice, but it doesn't hold for Java.... Sorry
(Just to be clear: I didn't say you should code like a pig either!)
Regarding the use of Vector and List, no, they are not the same thing (but again, you didn't understand that I was talking about utility)... I'm not going to tell him to use a TreeMap when he only needs a list...
Anyway. Let's not pollute his topic, he got his answer. -
Since when do we do embedded programming with high-level languages like Java?
Look at your mobile phone, your GPS, etc... J2ME is everywhere!
To say that arrays are not used in Java is a monstrous aberration!
Before creating high-level classes that fully utilize object-oriented programming, one needs a solid foundation, which involves lower-level classes with optimized basic operations!
But you are right on one point, this discussion is entirely off-topic relative to the subject.
-
-
If you do it this way, your list will contain the elements of the array twice.
First because you call your constructor with Arrays.asList(user) as a parameter
Then a second time with the for loop that adds the elements one by one.
--
Trust does not exclude control. -
First of all, thank you all for the time you have given me, and thank you for the solution, it’s exactly what I was looking for, (i'm happy)... it’s my first program where I use lists.