What Is One Dimensional Array in Data Structure?

//

Heather Bennett

A one-dimensional array is a data structure that stores a fixed-size sequence of elements of the same type. It is also known as a linear array or a vector. In simple terms, a one-dimensional array can be thought of as a list of items, where each item has a unique index or position.

Declaring and Accessing Elements in a One-Dimensional Array

To declare an array in HTML, you can use the <script> tag with the var keyword followed by the name of the array and square brackets to define its size. For example:

  
<script>
  var myArray = new Array(5);
</script>
  

In this example, we have declared an array called myArray with a size of 5 elements. Note that arrays are zero-indexed, which means the first element is accessed using the index 0.

You can access and modify elements in an array using their index. For example:

  
<script>
  var myArray = new Array(5);
  
  myArray[0] = "Hello";
  myArray[1] = "World";
  
  document.write(myArray[0] + " " + myArray[1]);
</script>
  

This code will output “Hello World” because we assigned values to the first two elements (index 0 and index 1) of the array and then printed them using the document.write() method.

Operations on One-Dimensional Arrays

Traversal: Traversing an array means accessing each element of the array exactly once. This can be done using a loop, such as a for loop or a while loop. For example:

  
<script>
  var myArray = [1, 2, 3, 4, 5];
  
  for (var i = 0; i < myArray.length; i++) {
    document.write(myArray[i] + " ");
  }
</script>
  

This code will output "1 2 3 4 5" by traversing the array and printing each element.

Insertion: Inserting an element into an array means adding a new element at a specific position within the array. This can be accomplished by shifting the existing elements to make room for the new element and then assigning its value to the desired index. For example:

  
<script>
  var myArray = [1, 2, 4, 5];
  
  // Inserting element at index 2
  for (var i = myArray.length -1; i >=2 ; i--) {
    myArray[i+1] = myArray[i];
   }
  
   myArray[2] = 3;
   
   // Printing the updated array
   for (var j =0 ; j

This code will output "1 2 3 4 5" by inserting the value "3" at index position "2" and shifting the subsequent elements to the right.

Deletion: Deleting an element from an array means removing an element from a specific position within the array. This can be done by shifting the elements after the deleted element to fill the empty space. For example:

  
<script>
  var myArray = [1, 2, 3, 4, 5];
  
  // Deleting element at index 2
  for (var i =2 ; i < myArray.length -1; i++) {
    myArray[i] = myArray[i+1];
   }
  
   myArray.length--;
   
   // Printing the updated array
   for (var j =0 ; j

This code will output "1 2 4 5" by deleting the element at index position "2" and shifting the subsequent elements to the left.

Advantages of One-Dimensional Arrays

  • One-dimensional arrays provide a simple and efficient way to store and access a sequence of values.
  • They allow random access to elements using their index.
  • Operations such as traversal, insertion, and deletion can be easily performed on one-dimensional arrays.

Conclusion

A one-dimensional array is a fundamental data structure that allows you to store and manipulate a fixed-size sequence of elements. By understanding how to declare, access, and perform common operations on one-dimensional arrays, you can effectively manage collections of data in your programs. Remember to use appropriate loops and indexing techniques for efficient traversal and manipulation of array elements.