Is the method of putting the ArrayList in the ArrayList very bad for the performance?

Asked 2 years ago, Updated 2 years ago, 28 views

ArrayList<T> list1;
ArrayList<T> list2;
ArrayList<T> list3;
ArrayList<T> list4;

This and

ArrayList<ArrayList<T>> list; 

I think this could play the same role (although I haven't done it myself yet) but is the method below different from the one above in terms of performance?

java

2022-09-21 20:31

2 Answers

There is no difference in performance, and you can use it according to the purpose.

If the variable you want is fixed, it is the right use to declare each variable as shown above.

ex) If you need two good and bad lists,

ArrayList<String> goodItems = new ArrayList<String>();
ArrayList<String> badItems = new ArrayList<String>();

When the number of variables you want is variable, you can use it as below.

ex) 1 list of all the lists held by N people

ArrayList<ArrayList<String>> numOfList = new ArrayList<ArrayList<String>>();     


2022-09-21 20:31

Are you asking the difference between multi-array and single-array?

If that's right, if you look at the link below, there is little difference, but if you look at the compiled byte code, there is one more instruction for multi-array to load objects, so it seems that multi-array appears slowly. http://stackoverflow.com/questions/2512082/java-multi-dimensional-array-vs-one-dimensional

If you're asking about the difference between making multiple ArrayList and making one and putting it in, I think the latter would be better because making one is beneficial in terms of memory.


2022-09-21 20:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.