Array (ADT)
The array is a basic abstract data type that holds an ordered collection of items accessible by an integer index. These items can be anything from primitive types such as integers to more complex types like instances of classes. Since it's an ADT, it doesn't specify an implementation, but is almost always implemented by an array (data structure) or dynamic array.
Unless otherwise specified, for the remainder of this wiki the word "array" will refer to the abstract data type and not the data structure.
Arrays have one property: they store and retrieve items using an integer index. An item is stored in a given index and can be retrieved at a later time by specifying the same index. The way these indices work is specific to the implementation, but you can usually just think of them as the slot number in the array that the value occupies. Take a look at the image below:
In this visual representation of an array, you can see that it has a size of six. Once six values have been added, no more can be added until the array is resized or something is taken out. You can also see that each "slot" in the array has an associated number. By using these numbers, the programmer can directly index into the array to get specific values. Note that this image uses one-based numbering. Most languages use zero-based numbering where the first index in an array is 0, not 1.
Python's list built-in provides array functionality. C, C++, C#, Java, and a few other languages all have similar syntax for working with arrays. Here's how you would initialize an array in Java and then assign a value to an index. The following code creates a new array with size 5 and sets index 2 equal to 4.
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 |
|
You can think about arrays as a group of numbered boxes that only hold one item each. Say we have 10 boxes. That means we can't store 11 items because that's more than the number of boxes we have. If I want to store my phone in box number 5, I can go directly there and place it in the box. Later, if I want to see what's in box number 5, I can again go directly there to look inside and see my phone.
(sumber) https://brilliant.org/