Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyGenericStack<E> {
- //char[] array=new char[10];
- E[] array = (E[])new Object[10];
- int size=0;
- boolean empty=true;
- public void push(E element) {
- if(size==array.length-1) {
- //System.out.println("The array is full");
- E[] newArray=(E[])new Object[array.length+5];
- for (int i=0; i<array.length;i++) {
- newArray[i]=array[i];
- }
- array=newArray;
- }
- array[size]=element;
- empty=false;
- size++;
- }
- public void pop(){
- if(empty==false) {
- array[size]=null;
- size--;
- }else {
- System.out.println("Can't pop an empty stack");
- }
- if(size==0) {
- empty=true;
- }
- }
- public E peek() {
- E element=array[size-1];
- return element;
- }
- public int size() {
- return size;
- }
- public boolean isEmpty() {
- return empty;
- }
- }
- public class Book {
- String title;
- String author;
- String category;
- public Book(String title, String author, String category) {
- }
- public String getCategory() {
- return category;
- }
- public String toString() {
- return title + " by " + author +", " + "category:" + category;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement