从C+;创建java泛型堆栈类+;堆栈类 #包括 #包括 使用名称空间std; 模板 类堆栈 { 公众: T arr[100]; int top; 堆栈() { top=-1; } 公众: 无效推力(T x) { 如果(顶部==99) cout

从C+;创建java泛型堆栈类+;堆栈类 #包括 #包括 使用名称空间std; 模板 类堆栈 { 公众: T arr[100]; int top; 堆栈() { top=-1; } 公众: 无效推力(T x) { 如果(顶部==99) cout,java,templates,generics,Java,Templates,Generics,导入java.util.EmptyStackException; 导入java.util.Vector 公共类堆栈扩展向量{ import java.util.EmptyStackException; import java.util.Vector; public class Stack extends Vector{ /** * Creates an empty Stack. */ public Stack() { } public E pop() { E ob


导入java.util.EmptyStackException;
导入java.util.Vector

公共类堆栈扩展向量{


import java.util.EmptyStackException;
import java.util.Vector;

public class Stack extends Vector{

 /**
 * Creates an empty Stack.
 */
public Stack() {

}


public E pop() {
    E   obj;
    int len = size();

    if (len == 0)
        throw new EmptyStackException();
    obj = elementAt(len - 1);
    removeElementAt(len - 1);
    return obj;
}


public void push(E e) {

    if(e==null)
        throw new NullPointerException();
    addElement(e);

}


public int size() {
    return elementCount;
}


public static void main(String args[]){


Stack<Integer>  integerStack= new Stack<Integer>();

//(7,1,3,3,5,1,2,4,3)
integerStack.push(7);
integerStack.push(1);
integerStack.push(3);
integerStack.push(3);
integerStack.push(5);
integerStack.push(1);
integerStack.push(2);
integerStack.push(4);
integerStack.push(3);


System.out.println("STACK WITH INTEGER ELEMENTS");
//Size
System.out.println("Size of the Stack is : "+integerStack.size());



// String Stack
Stack<String>  stringStack= new Stack<String>();

//("abc","def","acd","fgi","fth","lmn","zxy","cde","adr")
stringStack.push("abc");
stringStack.push("def");
stringStack.push("acd");
stringStack.push("fgi");
stringStack.push("fth");
stringStack.push("lmn");
stringStack.push("zxy");
stringStack.push("cde");
stringStack.push("adr");


System.out.println("STACK WITH STRING ELEMENTS");
//Size
System.out.println("Size of the Stack is : "+stringStack.size());



//pop
stringStack.pop();

//Size after pop
System.out.println("Size of the Stack After pop is : "+stringStack.size());
/**
*创建一个空堆栈。
*/
公共堆栈(){
}
公共E-pop(){
E obj;
int len=大小();
如果(len==0)
抛出新的EmptyStackException();
obj=元素at(len-1);
removeElementAt(len-1);
返回obj;
}
公共空间推送(E){
如果(e==null)
抛出新的NullPointerException();
增编(e);
}
公共整数大小(){
返回元素计数;
}
公共静态void main(字符串参数[]){
Stack integerStack=新堆栈();
//(7,1,3,3,5,1,2,4,3)
积分钉压(7);
积分钉压(1);
积分钉压(3);
积分钉压(3);
积分钉压(5);
积分钉压(1);
积分钉压(2);
积分钉压(4);
积分钉压(3);
System.out.println(“带整数元素的堆栈”);
//大小
System.out.println(“堆栈的大小为:”+integerStack.Size());
//字符串堆栈
Stack stringStack=新堆栈();
//(“abc”、“def”、“acd”、“fgi”、“fth”、“lmn”、“zxy”、“cde”、“adr”)
stringStack.push(“abc”);
stringStack.push(“def”);
stringStack.push(“acd”);
stringStack.push(“fgi”);
stringStack.push(“fth”);
stringStack.push(“lmn”);
stringStack.push(“zxy”);
stringStack.push(“cde”);
stringStack.push(“adr”);
System.out.println(“带字符串元素的堆栈”);
//大小
System.out.println(“堆栈的大小为:“+stringStack.Size()”);
//流行音乐
stringStack.pop();
//流行音乐后的尺寸
System.out.println(“pop后堆栈的大小为:“+stringStack.Size()”);
}

}

我希望这是一个家庭作业,否则你会重新发明堆栈!试着自己做,如果你在做这件事时有特殊问题,请在这里发布。这不是“为我编写代码”服务。我只需要帮助lyk wat更改我必须为java泛型类做的更改。请看一看源代码。

import java.util.EmptyStackException;
import java.util.Vector;

public class Stack extends Vector{

 /**
 * Creates an empty Stack.
 */
public Stack() {

}


public E pop() {
    E   obj;
    int len = size();

    if (len == 0)
        throw new EmptyStackException();
    obj = elementAt(len - 1);
    removeElementAt(len - 1);
    return obj;
}


public void push(E e) {

    if(e==null)
        throw new NullPointerException();
    addElement(e);

}


public int size() {
    return elementCount;
}


public static void main(String args[]){


Stack<Integer>  integerStack= new Stack<Integer>();

//(7,1,3,3,5,1,2,4,3)
integerStack.push(7);
integerStack.push(1);
integerStack.push(3);
integerStack.push(3);
integerStack.push(5);
integerStack.push(1);
integerStack.push(2);
integerStack.push(4);
integerStack.push(3);


System.out.println("STACK WITH INTEGER ELEMENTS");
//Size
System.out.println("Size of the Stack is : "+integerStack.size());



// String Stack
Stack<String>  stringStack= new Stack<String>();

//("abc","def","acd","fgi","fth","lmn","zxy","cde","adr")
stringStack.push("abc");
stringStack.push("def");
stringStack.push("acd");
stringStack.push("fgi");
stringStack.push("fth");
stringStack.push("lmn");
stringStack.push("zxy");
stringStack.push("cde");
stringStack.push("adr");


System.out.println("STACK WITH STRING ELEMENTS");
//Size
System.out.println("Size of the Stack is : "+stringStack.size());



//pop
stringStack.pop();

//Size after pop
System.out.println("Size of the Stack After pop is : "+stringStack.size());