LinkedList vs ArrayList case study for Android

I am returning a List from my class A. I would like to remove the first item from the list and add it as the last item to the same list. I did it this way.

myList.add(myList.get(0));
myList.remove(0);

The target equipment is Android OS. Should I code my class Aso that it returns ArrayListor LinkedList? What would be better for the following scenarios:

  • myList always contains 100 elements

  • myList always has 10 elements

Perhaps I see a problem when it is not there. Do you think that in this case I should not care about performance, since the size of the problem (for 1 and 2) is small?

I know that "premature optimization is the root of all evil." This is why I am embarrassed before changing my incarnation (since now my object Areturns an ArrayList).

+4
source share
1 answer

The short answer is , you should go for LinkedListif you often add / remove / update an element, especially for the first / last elements, since it contains a pointer to the first and last node

LinkedList O(1) , ArrayList O(n) . LinkedList . , :


, ArrayList write-once-read-many appenders, / .

enter image description here

, O(1), ( ) O(n).

, , bigoh post states:

Big-oh , - . , , , . , , Big-Oh - .

, LinkedList .

, / , , javaconceptoftheday.com: enter image description here


http://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

+4

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


All Articles