Home list List and Array Difference

List and Array Difference

Author

Date

Category

Strictly speaking, a connected list and array are various data structures that are not tied to a specific programming language.

Array

An array is a combination of the same type of data located continuously in memory. Access to the element is carried out by index for O (1) – we appeal directly to the desired area of ​​memory.

Related List

Access to the element in a connected list on average occupies o (n) by brute force elements in search of the desired one. Methods for access to elements differ in implementation and from programming language. For example, on Java in a standard class LinkedList , depending on the situation, the passage of elements can begin both from the beginning and from the end of the list. And the search for an item can be carried out both by index and compared to the elements.

The connected list requires large memory spending, other things being equal, due to storage of pointers to the next / previous elements and features of the internal implementation.

As for Python: According to Documentation :

Internally, A List Is Repretesented AS An Array; The Largest Costs Come from Growing Beyond The Current Allocation Size (BECAUSE EVERYTING MUST MOVE), OR FROM INSERTING OR DELETING SOMEWHERE NEAR THE BEGINNING (Because Everything After That Must Move).

As you can see, internally List is an array for Tuple – similar.


Answer 1, Authority 100%

It is impossible to say that there is some difference regardless of the programming language, since the author of each language can define these words as he wants.

Nevertheless, it seems to me, the difference between the collections list (List), the array (array) and the tuple (TUPLE) lies in the following characteristics:

  • fixed size – if the collection size is fixed, then you cannot add or remove the items dynamically:

    int [] x = new int [2];
    x [0] = 50;
    x [1] = 45;
    x [2] = 40; // Most likely there will be a mistake or uncertain behavior
    

    And if the size is not fixed, you can add items dynamically:

    list & lt; int & gt; x = new list & lt; int & gt; ();
    x.add (50);
    X.Add (45);
    x.add (40);
    
  • data variability – if the collection is changeable, then you can change the data after creation, and if unchangeable, then no.

Specific difference:

  • Size Fixed, Changeable: Array (Array)
  • Size Fixed, Unchanging: Tuple
  • size is not fixed, changeable: List (List)
  • size is not fixed, immutable: immutable list (Immutable List)

about memory and speed: it defines exactly on the language and a specific structure. For example, the complexity of access to the index element in the list may be O (1) (for example, C # List ), O (log (n)) (for example, a bunch), or O (n) (for example, a connected list ).

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions