ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

In Java, How do you decide when to use ArrayList and When to use LinkedList?

user-image
تم إضافة السؤال من قبل Salauddin Mohammad , Sr. Manager, Software Development , Aspen Technology Inc
تاريخ النشر: 2014/08/27
Mohd shahnawaz khan
من قبل Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution

LinkedList is not as popular as ArrayList but still there are situation where a LinkedList is better choice than ArrayList in Java. Use LinkedList in Java if:

 

1) Your application can live without Random access. Because if you need nth element in LinkedList you need to first traverse up to nth element O(n) and than you get data from that node.

 

2) Your application is more insertion and deletion driver and you insert or remove more than retrieval. Since insertion or removal doesn't involve resizing its much faster than ArrayList.

 

Alabbas Alhaj Ali
من قبل Alabbas Alhaj Ali , Senior Frontend Developer , Acomodeo

Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having10 elements, if you are inserting at index5, then you need to shift the indexes above5 to one more.

pardeep kumar
من قبل pardeep kumar , Senior R & D Engineer , Mavenir Systems

Use LinkedList when you need fast insertion and deletion whereas which is slow in ArrayList

Use ArrayList when you need fast iteration and fast random access whereas which is slow in LinkedList

OMAR ABDULWAHAB
من قبل OMAR ABDULWAHAB , programmer , knone

personally ; i don't care about speed,  computers is fast enough nowadays ......    :)-

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟