Data Structures In Java - Linear Array

Data structure means organizing the data by using models in the computer memory. We can represent the data in two ways - linear data structure and non-linear data structure. A linear data structure that represents a relationship between elements by successive memory location is known as an array, where as a linear data structure that represents a relationship between elements by a pointer and link is known as a linked list. Common non-linear data structures are tree, graph etc.

Linear Array (One dimension array)

A linear array is a list of finite numbers of elements stored in the memory. In a linear array, we can store only homogeneous data elements. Elements of the array form a sequence or linear list that can have the same type of data.

Each element of the array is referred by an index set. And, the total number of elements in the array list is the length of an array. We can access all these elements with the help of the index set.

For example, consider an array of employee names with the index set from 0 to 7 which contains 8 elements as shown in fig:

Data Structure 

Now, if we want to retrieve the name 'Alex' from the given array, we can do so by calling "employees [3]" and it gives the element stored in that location that is 'Alex' and so on.

We can calculate the length or a total number of elements of a linear array (LA) by the given formula:

Length (LA)=UB-LB+1

Here, UB refers to the upper bound, the largest index set of the given array list and LB referred to the lower bound, smallest index set of the given array list.

For example, we have an array of employees in which lower bound is 153 and the upper bound is 234, so we can calculate the total number of elements in the list by giving formula.


We can perform various operations on a linear array:

  • Traversing-  processing each element of the array list.
  • Inserting- adding new elements in the array list.
  • Deleting- removing an element from the array list.
  • Sorting- arranging the elements of the list in some sorting order.
  • Merging- combining the elements of two array list in a single array list.


Basically, an array is recommended when we have to perform operations like retrieving the data or reading the data. But, for the operations like insertion and deletion, array is not recommended.

Traversing Linear Array

We can process each element of an array with the help of an index set.

Consider a Linear Array(LA) list given with lower bound(LB) and upper bound(UB) that contains 'n' number of elements. We can traverse the list with the given algorithm.


  1. (initialized counter) K=LB
  2. Repeat while k<=UB
  3. Process LA[k]
  4. K=k+1
    (End of loop)
  5. Exit


  1. public class Traverse {  
  2.     public static void main(String args[]) {  
  3.         String[] employee = {  
  4.             "john",  
  5.             "alex",  
  6.             "rone",  
  7.             "mike",  
  8.             "zeny",  
  9.             "crist"  
  10.         };  
  11.         for (int k = 0; k < employee.length; k++) {  
  12.             System.out.println(employee[k]);  
  13.         }  
  14.     }  
  15. }  


Data Structure 

Inserting and Deleting

Array insertion of an element at the end of the list is quite simple, we can do so by providing the unallocated space to the new element. On the other hand, if we need to insert an element in the middle of an array, lots of internal shifting is required to insert the new element. i.e. half of the elements must be moved downward to the new location to enter the new element.

Similarly, deletion of the element from the end of the array is quite simple but if we need to delete an element from the middle of the array, then on average, half of the elements must be moved upward in order to fill the blank space, after deleting the element from the specified location.

Algorithm for insertion of element

Here, LA a linear array with n number of elements, where K is a positive integer i.e. K<=n. This algorithm inserts an element item into the Kth position.

  1. (Initialized counter) J=n
  2. Repeat step 3 & 4 while j>=k
  3. [Move downward] set LA[J+1]=LA[J]
  4. [Decrement counter] set j=j-1;
    End of step 2
  5. [Insert element] set LA[k]=ITEM
  6. [Reset n] set n=n+1
  7. Exit


  1. public class InsertElement {  
  2.     public static void main(String args[]) {  
  3.         int n = 6;  
  4.         String name[] = new String[n];  
  5.         name[0] = "mike";  
  6.         name[1] = "rick";  
  7.         name[2] = "max";  
  8.         name[3] = "christ";  
  9.         name[4] = "larry";  
  10.         String new_item = "jack";  
  11.         int k = 2;  
  12.         int j = n - 1;  
  13.         // elements before inserting new elements  
  14.         System.out.println("elements before inserting new elements");  
  15.         for (int i = 0; i < n; i++) {  
  16.             System.out.println("position" + "(" + i + ") " + name[i]);  
  17.         }  
  18.         System.out.println("elements after inserting new elements");  
  19.         while (j >= k) {  
  20.             name[j] = name[j - 1];  
  21.             j = j - 1;  
  22.         }  
  23.         name[k] = new_item;  
  24.         for (int i = 0; i < n; i++) {  
  25.             System.out.println("position" + "(" + i + ") " + name[i]);  
  26.         }  
  27.     }  
  28. }  


Data Structure
Algorithm for deleting an element

Here, LA is a linear array with 'n' number of elements and K is a positive integer number such that k<=N. This algorithm removes the element from the Kth position.

  1. Set Item=LA[k]
  2. Repeat for J=k to N-1
  3. [move J+1st element upward] LA[J]=LA[J+1]
    [End of step 2]
  4. [Reset the number of element] N=N-1
  5. Exit


  1. public class Deleting {  
  2.     public static void main(String args[]) {  
  3.         int n = 5;  
  4.         String name[] = new String[n];  
  5.         name[0] = "mike";  
  6.         name[1] = "rick";  
  7.         name[2] = "max";  
  8.         name[3] = "christ";  
  9.         name[4] = "larry";  
  10.         String del_item;  
  11.         int k = 2;  
  12.         // elements before inserting new elements  
  13.         System.out.println("elements before inserting new elements");  
  14.         for (int i = 0; i < n; i++) {  
  15.             System.out.println("position" + "(" + i + ") " + name[i]);  
  16.         }  
  17.         System.out.println("elements after deleting the elements");  
  18.         del_item = name[k];  
  19.         for (int j = k; j < n - 1; j++) {  
  20.             name[j] = name[j + 1];  
  21.         }  
  22.         n = n - 1;  
  23.         for (int i = 0; i < n; i++) {  
  24.             System.out.println("position" + "(" + i + ") " + name[i]);  
  25.         }  
  26.     }  
  27. }  


Data Structure

Up Next
    Ebook Download
    View all
    View all