The difference in performance and memory consumption between Array and ArrayList

I find that I can do almost anything easily and with more control using ArrayListthan with the array in my daily coding. So I ask:

  • Is it advisable to avoid arrays if I can use instead ArrayList?
  • Is there something I need to keep in mind when I replace an array ArrayList?
+4
source share
4 answers

, . JVM. arrays , List. array, , . , List , . generics. covariant Generics invariant, Arrays and Generics don't mix, Joshua Bloch List over array Effective Java II 25. List, .

+5

Array ArrayList , . , ArrayList, , , ArrayList. , , ArrayList, , , , , .

, , List over Array . - . , List , (ArrayList, LinkedList ..), , .

, , , , Java.

, - :

List<T> list = foo.getList();

getList() ArrayList, , LinkedList, , , , , , - .

+3
  • , List .

    , . " Big-O". , , yourList.toArray(yourArray). , Arrays (Arrays.asList(yourArray)). List , , , , , .

  • -, , ( ).

    , , - . , . (, 100 ) 1 , , () 100 . , , ; .

+3

ArrayList , , , Arraylist. , ArrayList , , , , , , ArrayList .

+1

Source: https://habr.com/ru/post/1568791/


All Articles