Home > Back-end >  Custom Java Generic ArrayList Initialization out of bounds
Custom Java Generic ArrayList Initialization out of bounds

Time:09-16

I'm working on a custom implementation of a generic ArrayList in Java. I'm not allowed to tamper with the main function. How could I fix the initialization issue without messing with the main?

import java.util.ArrayList;

public class MyStack {
  private ArrayList<Object> list;
  
  MyStack() {
    list = new ArrayList<Object>();
  }

  MyStack(ArrayList<Object> list) {
    
    this.list = list;
  }
  
  MyStack(Object[] array) {
    
    list = new ArrayList<Object>();
    for (int i = 0; i < array.length; i  ) {
      list.add(array[i]);
    }
  }

  MyStack(MyStack stack) {
        list = new ArrayList<Object>();
    for (int i = 0; i < stack.getSize(); i  ) {
      list.add(stack.get(i));
    }
  }

  MyStack(int capacity) {
    
    list = new ArrayList<Object>(capacity);
  }

  public Object get(int index) {
    
    return list.get(index);
  }


  public boolean isEmpty() {
    
    if(list.get(0) == null){
      return true;
    }
    return false;
  }

  public void push(Object o) {
    
    list.add(o);
  }

  public Object pop() {
   
    int size = getSize();
    Object a = this.list;
    if(!isEmpty()){
      Object o = list.get(list.size() - 1);
      return o;
    }
    return a;
  }

  public Object peek() {
    
    int size = getSize();
    if(size <= 0){ return null; }

    Object o = list.get(size-1);
    return o;
  }

  public int getSize() {
    return list.size();
  }

  @Override /** Override the toString in the Object class */
  public String toString() {
    return "stack: "   list.toString();
  }

  public static void main(String[] args) {
    MyStack stack = new MyStack();
    // get args from command line
    for (int i = 0; i < args.length; i  ) {
      stack.push(args[i]);
    }
    System.out.println(stack);
    while (!stack.isEmpty()) {
      System.out.print(stack.pop()   " ");
    }
  }
}

It's a generic implementation of the ArrayList. When compiling I get this error:

stack: []
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
        at java.base/java.util.Objects.checkIndex(Objects.java:372)
        at java.base/java.util.ArrayList.get(ArrayList.java:459)
        at MyStack.isEmpty(MyStack.java:52)
        at MyStack.main(MyStack.java:104)

I'm confused as to how index 0 would be out of bounds? What could I do to account for an ArrayList with 0 dimensions?

CodePudding user response:

You seem to expect your ArrayList to return null if you ask for a nonexistent element (get(0)), but that's not the behavior; it throws instead. To check whether a list is empty, you can use list.isEmpty() or list.size() == 0.

CodePudding user response:

  public boolean isEmpty() {
    
    if(list.get(0) == null){
      return true;
    }
    return false;
  }

If the list is empty, list.get(0) will throws IndexOutOfBound exception.

Why not just return list.isEmpty()?

  public boolean isEmpty() {
    return list.isEmpty();
  }
  •  Tags:  
  • java
  • Related