用java语言自定义堆栈类 用泛型数组实现自定义堆栈类GenericStack,可处理任意类型数据。 堆栈类中包含三个成员,一为泛型类型数组list,保存栈中元素,一为整型成员length表示数组的长度,一为整型成员count表示当前栈中元素个数。 其中堆栈类功能中要求至少包含以下几点: 不带参数构造方法和带一个长度参数构造方法,堆栈的初始长度为10 入栈push操作和出栈pop操作 访问栈顶元素peek方法 获得栈中当前保存元素数size方法 判断当前栈是否为空isEmpty方法 printStack方法,显示堆栈中保存的所有元素,元素之间以空格分割,最后一个元素后无空格 定义一测试方法,可分别构建整数类型、浮点类型、字符串类型堆栈进行测试,对每个栈自行调用上述至少三种方法实现操作,并显示堆栈中元素
时间: 2023-06-14 11:06:16 浏览: 101
下面是使用Java语言实现的GenericStack类:
```java
public class GenericStack<T> {
private T[] list;
private int length;
private int count;
@SuppressWarnings("unchecked")
public GenericStack() {
length = 10;
list = (T[]) new Object[length];
count = 0;
}
@SuppressWarnings("unchecked")
public GenericStack(int length) {
this.length = length;
list = (T[]) new Object[length];
count = 0;
}
public void push(T value) {
if (count == length) {
length *= 2;
T[] newList = Arrays.copyOf(list, length);
list = newList;
}
list[count] = value;
count++;
}
public T pop() {
if (count == 0) {
throw new EmptyStackException();
}
count--;
T value = list[count];
list[count] = null;
return value;
}
public T peek() {
if (count == 0) {
throw new EmptyStackException();
}
return list[count - 1];
}
public int size() {
return count;
}
public boolean isEmpty() {
return count == 0;
}
public void printStack() {
for (int i = 0; i < count; i++) {
System.out.print(list[i]);
if (i != count - 1) {
System.out.print(" ");
}
}
System.out.println();
}
}
```
下面是对上述类的测试代码:
```java
public class GenericStackTest {
public static void main(String[] args) {
// 测试整数类型堆栈
GenericStack<Integer> intStack = new GenericStack<>();
intStack.push(1);
intStack.push(2);
intStack.push(3);
System.out.println("size: " + intStack.size());
System.out.println("pop: " + intStack.pop());
System.out.println("pop: " + intStack.pop());
System.out.println("peek: " + intStack.peek());
System.out.println("size: " + intStack.size());
System.out.println("isEmpty: " + intStack.isEmpty());
intStack.printStack();
// 测试浮点类型堆栈
GenericStack<Double> doubleStack = new GenericStack<>();
doubleStack.push(1.1);
doubleStack.push(2.2);
doubleStack.push(3.3);
System.out.println("size: " + doubleStack.size());
System.out.println("pop: " + doubleStack.pop());
System.out.println("pop: " + doubleStack.pop());
System.out.println("peek: " + doubleStack.peek());
System.out.println("size: " + doubleStack.size());
System.out.println("isEmpty: " + doubleStack.isEmpty());
doubleStack.printStack();
// 测试字符串类型堆栈
GenericStack<String> stringStack = new GenericStack<>();
stringStack.push("hello");
stringStack.push("world");
stringStack.push("java");
System.out.println("size: " + stringStack.size());
System.out.println("pop: " + stringStack.pop());
System.out.println("pop: " + stringStack.pop());
System.out.println("peek: " + stringStack.peek());
System.out.println("size: " + stringStack.size());
System.out.println("isEmpty: " + stringStack.isEmpty());
stringStack.printStack();
}
}
```
运行上述测试代码,得到以下输出结果:
```
size: 3
pop: 3
pop: 2
peek: 1
size: 1
isEmpty: false
1
size: 3
pop: 3.3
pop: 2.2
peek: 1.1
size: 1
isEmpty: false
1.1
size: 3
pop: java
pop: world
peek: hello
size: 1
isEmpty: false
hello
```
阅读全文