将泛型实现到原始堆栈

时间:2017-05-14 03:18:39

标签: java generics stack primitive

我对如何在java中实现泛型一无所知,所以我希望能得到一些帮助将我的堆栈(下面)的原始实现转换为使用泛型的程序(假设它可以只改变一些事情,而不是如何写一个完全不同的程序)。

以下是我的代码:

import java.io.*;
import java.util.*;

public class LinkedListStack {

    public static Stack<Integer> stk = new Stack<Integer>();
    public static int min, push, top;

    public static void main(String[] args) {        

        //initialize random integer generator
        Random rand = new Random();

        System.out.println("Stack empty --> top = null, min = null");
        stackPush(rand.nextInt(50));

        //first value in the stack is the minimum until a smaller integer is pushed
        min = stk.peek();

        stackPush(rand.nextInt(50));
        stackPush(rand.nextInt(50));
        stackPop();
        stackPush(rand.nextInt(50));
        stackPush(rand.nextInt(50));
        stackPush(rand.nextInt(50));
        stackPush(rand.nextInt(50));
        stackPop();
        stackPop();
        stackPop();
        stackPop();
        stackPop();
        stackPop();
        stackPop();

        if (!stk.isEmpty()) {
            System.out.print("\nFinal stack: ");
            for(int x : stk) {
                System.out.print(x + " ");
            }
        } else {
            System.out.print("\nStack is empty!");
        }

        System.out.println();
    }

    public static int stackPush(int pushInt) {
        try {
            stk.push(pushInt);
            if (stk.peek() < min) {
                min = stk.peek();
            }
            top = stk.peek();
            System.out.println("Push " + pushInt + " --> top = " + top +  ", min = " + min);

        } catch (EmptyStackException e){
            System.out.println("ERROR");
        }

        return pushInt;
    }

    public static void stackPop() {
        try {
            stk.pop();
            if (stk.peek() < min) {
                min = stk.peek();
            }
            top = stk.peek();
            System.out.println("Pop --> top = " + top +  ", min = " + min);

        } catch (EmptyStackException e) {
            System.out.println("Stack already empty!");
        }
    }
}

2 个答案:

答案 0 :(得分:3)

首先,你的班级不应该是静态的来完成这个。它也不应该通过公共字段或通过命名(例如LinkListStack

)公开其底层实现

相反,您可以创建一个类,例如

class MyStack<E> {    
    private final Stack<E> wrapped = new Stack<E>();

    public void push(E element) {
        wrapped.push(e);
    }

    public E pop() {
        return wrapped.pop();
    }
}


public class Program {

     public static void main(String[] args) {        

     System.out.println("Stack empty --> top = null, min = null");

     MyStack<String> stack = new MyStack<>();

     stack.push("hello");

     stack.push("world");
}

注意: Java具有内置堆栈并且重新发明轮子是不好的做法,除非纯粹用于教育/探索性编程。这个内置的Stack实际上用于提供底层的包装值,它构成了我们实现的支柱。

答案 1 :(得分:1)

您可以使堆栈具有通用性,但是您必须决定如何实现比较功能,因为您看起来在任何给定时间都保持对堆栈中最小元素的引用。

如果你使它成为通用的,你必须考虑任何可能类型的最小元素。现在你正在使用整数,并且比较很容易,因为整数本质上是可比较的。

我会做以下事情:

public class LinkedListStack<T extends Comparable<T>> {
    public static Stack<T> stk = new Stack<T>();
    public static T min, push, top;

这也意味着你的push / pop方法必须使用compareTo而不是直接的整数比较,例如

 if (stk.peek().compareTo(min) < 0) {
            min = stk.peek();
 }

请记住,如果您计划在此堆栈中使用自己的类型,则必须使它们具有Comparable并实现compareTo方法。