Implement ArrrayLinearList without using In-built functions in Java

I am up with another tutorial of data structures in Java. In this tutorial, we will learn how to Implement ArrrayLinearList without using In-built functions in Java. It means that we will hard code all the small functions. But we will not take the help of a library of Java. As if we use library functions which are In-built, it will be very easy to create it.

So let’s start with the tutorial. We begin by creating a class named ArrayLinearList. So we define an Integer array named element. And another variable named size which stores the size of the defined array.

We have created user-defined functions like -:

  • isEmpty()
  • size()
  • checkIndex()
  • get()
  • indexOf()
  • add()
  • remove()
  • toString()

How to Implement ArrrayLinearList without using In-built functions in Java

We will discuss the above mentioned functions one by one. So I will try explain the usability of every function. Because here each function carries different operation and there is a link between these functions.

import java.util.*;
public class ArrayLinearList
{
protected Integer[] element;
protected int size;

public ArrayLinearList(int capacity)
{
  if(capacity<1)
    throw new IllegalArgumentException("initial capacity must be >=1");
  element = new Integer[capacity];
}
public boolean isEmpty()
{
  return size==0;
}
public int size()
{
  return size;
}
public void checkIndex(int index)
{
  if(index<0 || index> size)
    throw new IndexOutOfBoundsException("index="+index+"size"+size);
}
public Integer get(int index)
{
  checkIndex(index);
  return element[index];
}
public int indexOf(Integer _Element)
{
  for(int i=0;i<size;i++)
  {
    if(element[i]==_Element)
      return i;

}
  return -1;
}
public void add(int index, Integer _Element)
{
  if(index<0)
    throw new IndexOutOfBoundsException("index="+index+"size"+size);
  if(size==element.length)
  {
     Integer[] newArray  = new Integer[2*size];
    System.arraycopy(element, 0, newArray, 0, element.length);
    element= newArray;
    }
  for(int i = size-1;i>=index;i--)
    element[i+1]=element[i];
  element[index]= _Element;
size++;
}
public Object remove(int index)
{
  checkIndex(index);
  Integer removed = (Integer)element[index];
  for(int i=index+1;i<size;i++)
  {
    element[i-1]=element[i];
  }
  size=size-1;
  element[size]=null;
return removed;
}
public String toString()
{
  StringBuffer s = new StringBuffer("[");
  for(int i=0;i<size;i++)
    if(element[i]==null)
      s.append("0");
    else
      s.append(element[i].toString()+", ");
    if(size>0)
      s.delete(s.length()-2,s.length());
    s.append("]");
return new String(s);
}
public static void main(String[] args)
{
  Scanner sc = new Scanner(System.in);
  System.out.println("Please enter the size of ArrayList");
  int p = sc.nextInt();
  ArrayLinearList b = new ArrayLinearList(p);
  int c= 1;
  while(c>=1)
  {
    System.out.println("\nEnter your choice:-\n1.Add\n2.delete\n3.dispaly the array\n4.Exit");
    int a = sc.nextInt();
    if(a==4)
      break;
switch(a)
{
case 1:
  int q= sc.nextInt();
  Integer m = sc.nextInt();
  b.add(q,m);
  break;
case 2:
  System.out.println("Enter the index for deleting the object");
  int k = sc.nextInt();
  System.out.println("The deleted object is "+b.remove(k));
  break;
case 3:
  System.out.println("the total elements:-\n"+b.toString());
  break;
default:
  System.out.println("Please enter correct input..");
}
}
}
}

We have created ArrayLinearList class to create an array of entered size by the user. So if our size given by the user is less than 1. It will give exception as the error. We have created the isEmpty function to check whether the array is empty or not. Size function returns the size of the array.

We have created a checkIndex function to check whether the index entered by the user is less than our array size. So if it not then it will give index out of bound exception. Get function is used to get the element at that particular index.

The IndexOf function returns the index of a value in our array. But returns -1 if the element is not present in our array. Add function carries two parameters index and value. It basically adds a particular element at the index which we want in our array.

StringBuffer creates a memory space in our ram. Append is used to add values to our array and .delete removes the element from our array. We have created a toString function just to print all elements of our array in a sequential manner.




I hope you understood the code well. Feel free to ask your doubts in the comments section below.

You may also read,


Leave a Reply

Your email address will not be published. Required fields are marked *