/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/MyStack.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.calebfontenot.mp5_calebfontenot;
import java.util.ArrayList;
public class MyStack implements Cloneable {
private ArrayList<Object> list = new ArrayList<>();
public boolean isEmpty() {
return list.isEmpty();
}
public int getSize() {
return list.size();
}
public Object peek() {
return list.get(getSize() - 1);
}
public Object pop() {
Object o = list.get(getSize() - 1);
list.remove(getSize() - 1);
return o;
}
public void push(Object o) {
list.add(o);
}
@Override
public String toString() {
return "stack: " + list.toString();
}
@Override
public Object clone() throws CloneNotSupportedException {
MyStack clonedStack = (MyStack) super.clone();
ArrayList<Object> newList = new ArrayList<>(list);
clonedStack.list = newList;
return clonedStack;
}
public static void main(String[] args) throws CloneNotSupportedException {
MyStack stack = new MyStack();
final int STACK_SIZE = 50;
for (int i = 0; i < STACK_SIZE; ++i) {
stack.push(i + 1);
}
System.out.println(stack.peek());
MyStack clonedStack = (MyStack) stack.clone();
System.out.println(clonedStack.peek());
}
}