-2

How to change the following class to a class that uses ArrayList and LinkedList? I have to change all of the code in this class.

I am confused about ArrayList and LinkedList

import java.util.ArrayList;

public class ArrayStack {
    private int maxsize;
    private int top;
    private int[] items;

    public ArrayStack(int maxsize) {
        if (maxsize <= 0)
            throw new ArrayStackException("Stack size must be positive");
        items = new int[maxsize];
        this.maxsize = maxsize;
        top = 0;
    }

    public void push(int item) {
        if (top == items.length)
            throw new ArrayStackException("Overflow Error");
        items[top]=item;
        top++;
    }

    public int pop() {
        if (isEmpty()) 
            throw new ArrayStackException("Underflow Error");
        return items[--top];
    }

    public boolean isEmpty() {
        return (top==0); 
    }

    public static class ArrayStackException extends RuntimeException {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public ArrayStackException(String message) {
            super(message);
        }
    }

    public static void main (String[] a) {
        ArrayStack stack = new ArrayStack(3);
        stack.push(1);
        stack.push(2);
        stack.push(3);

        // srtack.push(4); //over flow error

        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }
}

give me some explanations on this problems.

1 Answers1

1

ArrayList: ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

Example:

// Java program to demonstrate working of ArrayList in Java
import java.io.*;
import java.util.*;

class arrayli
{
    public static void main(String[] args)
                       throws IOException
    {
        // size of ArrayList
        int n = 5;

        //declaring ArrayList with initial size n
        ArrayList<Integer> arrli = new ArrayList<Integer>(n);

        // Appending the new element at the end of the list
        for (int i=1; i<=n; i++)
            arrli.add(i);

        // Printing elements
        System.out.println(arrli);

        // Remove element at index 3
        arrli.remove(3);

        // Displaying ArrayList after deletion
        System.out.println(arrli);

        // Printing elements one by one
        for (int i=0; i<arrli.size(); i++)
            System.out.print(arrli.get(i)+" ");
    }
}

LinkedList: Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.

The important points about Java LinkedList are:

  • Java LinkedList class can contain duplicate elements.
  • Java LinkedList class maintains insertion order.
  • Java LinkedList class is non synchronized.
  • In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
  • Java LinkedList class can be used as list, stack or queue.

Example:

import java.util.*;  
public class TestCollection7{  
 public static void main(String args[]){  

  LinkedList<String> al=new LinkedList<String>();  
  al.add("Ravi");  
  al.add("Vijay");  
  al.add("Ravi");  
  al.add("Ajay");  

  Iterator<String> itr=al.iterator();  
  while(itr.hasNext()){  
   System.out.println(itr.next());  
  }  
 }  
}  

For more differences, You can refer https://www.javatpoint.com/difference-between-arraylist-and-linkedlist

Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26