将泛型实现到原始堆栈
Implementing generics to a primitive stack
我对如何在 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!");
}
}
}
首先,你的 class 不应该是静态的来完成这个。它也不应该通过 public 字段或通过 LinkListStack
等命名公开其底层实现
相反,您可以创建 class 例如
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 有一个内置的堆栈,重新发明轮子是不好的做法,除非纯粹是为了 educative/exploratory 编程。这个内置 Stack 实际上用于提供基础 wrapped 值,它构成了我们实现的 backbone。
您可以使堆栈通用,但您必须决定要如何实现比较功能,因为看起来您在任何给定时间都在维护对堆栈中最小元素的引用。
如果将其设为通用,则必须考虑任何可能类型的最小元素是什么。现在您正在使用整数并且比较很容易,因为整数本质上是可比较的。
我会做如下事情:
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();
}
请记住,如果您打算在此堆栈中使用您自己的类型,则必须使它们具有可比性并实施 compareTo 方法。
我对如何在 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!");
}
}
}
首先,你的 class 不应该是静态的来完成这个。它也不应该通过 public 字段或通过 LinkListStack
相反,您可以创建 class 例如
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 有一个内置的堆栈,重新发明轮子是不好的做法,除非纯粹是为了 educative/exploratory 编程。这个内置 Stack 实际上用于提供基础 wrapped 值,它构成了我们实现的 backbone。
您可以使堆栈通用,但您必须决定要如何实现比较功能,因为看起来您在任何给定时间都在维护对堆栈中最小元素的引用。
如果将其设为通用,则必须考虑任何可能类型的最小元素是什么。现在您正在使用整数并且比较很容易,因为整数本质上是可比较的。
我会做如下事情:
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();
}
请记住,如果您打算在此堆栈中使用您自己的类型,则必须使它们具有可比性并实施 compareTo 方法。