DEV Community

Cover image for Array Data Structure, What Is It?
Peyton Strahan
Peyton Strahan

Posted on

Array Data Structure, What Is It?

Intro:
Before I can talk about arrays, I must first explain what data structures even are. Data structures are different ways of storing and formatting data that affect how efficiently said data can be altered, found, and obtained. Data structures also determine the relationship between data. Nearly all forms of complex code used today utilize the data-storing, data-organizing, and other properties of data structures in order to achieve higher amounts of complexity while maintaining the ability to quickly process data and carry out tasks efficiently. Since data structures are just general methods for interacting with data, they can be implemented with nearly all coding languages and programs. Now that you have a general idea of what data structures are, let's focus on the array data structure.

What is the Array Data Structure?:
The array data structure, not to be confused with the array data type in JavaScript or any other language, is a static and linear data structure used to store elements (aka, data) of the same type within a contiguous memory block of your computer's memory. Somewhat similar to arrays in JavaScript, the array data structure contains data within cells (which are basically designated spots within your computer's data) that can be accessed through utilizing the corresponding index number of the cell (which starts at 0 and ends at a number equivalent to the number of cells in the array minus 1):

Image description

Unlike the arrays in JavaScript, the array data structure can only store data of the same type:

Image description

The array data structure is also inefficient and slow when it comes to resequencing values in the array, and it has a predetermined amount of usable data storage that cannot be changed after creation. Long story short, the array data structure is basically a much less flexible version of JavaScript's array data type, but it is a general method that can be used in more coding languages than just JavaScript.

Relatable Visual Example:
Think of an array data structure as one of those pill box organizers that holds your pills for each day of the week or month:
Image description
Just like an array, this pill box organizer holds only one type of thing (usually pills) in an organized series, has a maximum amount of items/item sets it can hold determined by the number of cells that the box was created with, and has indexes for each cell in the box (Sunday through Saturday). Similar to how mixing data types in an array will result in an error, mixing pills with other non-pill items in the pill box organizer will most likely result in you losing your job at the nursing home.

Code Example:
In order to replicate the array data type, let's imagine that we can only use my custom inserter function to alter the array. Methods like ".push", ".pop", ".shift", etc. can't be used in this situation. First, I created an object called "strictArray" and gave it two key value pairs:

let strictArray = {
  arr: [,,,,,],
  type: "number"
};

console.log(strictArray.arr.length);
Enter fullscreen mode Exit fullscreen mode

The first key-value pair consists of a key called "arr", which contains an array full of commas. Each comma adds an undefined variable to the array, increasing the array's length by one each time. Since the array has five commas, the console log would display a length of five. The second key says "type", and it contains a string determining what type of data is allowed to be inserted into the string. Since the type is "number", only numbers can be inserted into the array. To replicate how the arrays using the array data structure would have their values altered, I made the inserter function:

function inserter(item, index) {
  if(strictArray.type === typeof(item) && index < strictArray.arr.length && index >= 0){
    strictArray.arr[index] = item;    console.log(strictArray.arr);
  } else{
  console.log("error");
  }
}
Enter fullscreen mode Exit fullscreen mode

The inserter function takes in a value that you want to put into the array and an array index that defines exactly where in the array you wish to insert your value. In order for your insertion request to go through and successfully alter the array, it must first adhere the inserter function's criteria:

  1. The data type of the value must match the predetermined allowed data type for what can be put into the array (strictArray.type).

  2. The index of the item to be replaced must already exist in the array.

If the arguments defined in the function call do not pass these criteria, then the array will be unchanged and an error will be logged to the console. Now let's look at some examples of how this function would be called:

inserter(5, 4);
Enter fullscreen mode Exit fullscreen mode

The above function call changes the array because it meets the inserter function's criteria. The resulting array would be [undefined, undefined, undefined, undefined, 5].

inserter(5, 25);

inserter(9, -4);
Enter fullscreen mode Exit fullscreen mode

 
The two above function calls don't change the function because they reference an index that's not already in the array.

inserter("donkey", 4);
Enter fullscreen mode Exit fullscreen mode

The above function call fails because it tries to insert a string into the array, which doesn't match up with the array's required data type of "number".

At the most basic level, you can only assign elements at an already-existing index in the array to a new value as long as the data type of that value matches the pre-determined data type of the strictArray (which is the data type "number" in this case).

Conclusion:
All in all, the array data structure a simple but semi-robust data structure that is considered by some to be one of the main components of all data structures due to its ability to implement most other forms of data structures. While it may not be very lenient with what you can do with it on an individual level, the array data structure exceeds at its job of storing and organizing a series of data of the same type...and that's all it needs to do to be a useful asset to programmers of any type.

Links to Resources Used:
-https://medium.com/javarevisited/7-javascript-data-structures-you-must-know-95123122cc39
-https://www.geeksforgeeks.org/applications-advantages-and-disadvantages-of-array-data-structure/
-https://www.geeksforgeeks.org/array-data-structure/
-https://www.educative.io/courses/data-structures-coding-interviews-cpp/what-is-an-array
-https://www.freecodecamp.org/news/data-structures-101-arrays-a-visual-in

Top comments (0)