Difference between array and lists

 What is the difference between array and lists?



Welcome to my first (short)article about the difference between array and list, but before we talk for this, let's go understand one for one 👇:


Array

The array stores variables of the same data types in an orderly manner, the data in the array can be accessed by parameters as pointers to the position of the item, more precisely called an index. It have a invariable size, necessary the specification in your initialization.



 Lists

The list has a node that stores the data type of that item and points to the next item, can be linked or doubly linked:

  1. Linked: In the linked list, the node pointer always points to the next item in the list until finished.
  2. Doubly Linked: In the double linked list, the node storage information points to the last and next item in the list.

 vs 


 

 

Difference between array and lists

 The main difference between array and lists it the capacity of the lists in the storage differents data types and the array have more performance in cases of the structure have a fixed length. But let's go to analyze two situations:

  1.  You want to insert an item in the middle
    • Array: Will need to copy all elements to the right of the point where you want to place the item + 1. Lost of performance 😰
    • List: Will need altering the pointer of last for the new item and altering the pointer o new item for the next item in the list, this is more effectively 👍
  2. You want to know the third element:
    • List: Will need to get in the node of first position, the pointer for the second position and in the second position get the pointer for the third position. Imagine if it was the position 100º? 😆
    • Array: Based in the first position, just jump the two firsts elements and is there, the wanted item. More, more easily! 😉

Conclusion

The most interesting method is the best for your situation, this is variable! The array is fixed size, losing in dynamic to list, but array is more operational than the list, you will need to navigate through the entire structure if you need a specific item!

Therefore is necessary analyzes what is the best method for use in the case. Good Luck Dev 👊👋



 

- Marcos Marques

 

 

 

Comentários

Postagens mais visitadas