我能';我不能让我的泛型类在Java中工作
下面的执行包含三个方法我能';我不能让我的泛型类在Java中工作,java,Java,下面的执行包含三个方法plus向类中添加字符串,plus将其删除,empty检查并返回true(如果没有存储更多字符串) private static void test() { Stack<String> stack = new Stack<String>(); stack.plus("hello1"); stack.plus("hello2"); stack.plus("hello3"); stack.plus("hello4"
plus
向类中添加字符串,plus
将其删除,empty
检查并返回true(如果没有存储更多字符串)
private static void test() {
Stack<String> stack = new Stack<String>();
stack.plus("hello1");
stack.plus("hello2");
stack.plus("hello3");
stack.plus("hello4");
while (!stack.empty()) {
System.out.println(stack.minus());
}
stack.plus("a1");
stack.plus("a2");
stack.plus("a3");
stack.plus("a4");
stack.minus();
stack.minus();
stack.plus("a5");
stack.plus("a6");
while (!stack.empty()) {
System.out.println(stack.minus());
}
}
@SuppressWarnings("hiding")
public class Stack<String> {
private String e;
public void plus(String e) {
this.e= e;
}
public String minus() {
return e;
}
public boolean empty() {
if(e != null) {
}return false;
}
}
private静态无效测试(){
堆栈=新堆栈();
堆栈加上(“hello1”);
stack.plus(“hello2”);
堆栈加(“hello3”);
堆栈加上(“hello4”);
而(!stack.empty()){
System.out.println(stack.减号());
}
堆栈加上(“a1”);
堆栈加上(“a2”);
堆栈加上(“a3”);
堆栈加上(“a4”);
stack.减号();
stack.减号();
堆栈加上(“a5”);
堆栈加上(“a6”);
而(!stack.empty()){
System.out.println(stack.减号());
}
}
@抑制警告(“隐藏”)
公共类堆栈{
私有字符串e;
公共void plus(字符串e){
这个。e=e;
}
公共字符串减号(){
返回e;
}
公共布尔空(){
如果(e!=null){
}返回false;
}
}
输出应为:
你好
你好
你好
你好
a6
a5
a2
a1
目前,我的程序一直在“hello4”上无限循环,我不太明白如何修复我的
empty
函数。我怀疑方法是我的主要问题。您似乎误解了泛型的语法。在堆栈类中,泛型参数String
的行为更像一个变量,而不像String类
//DataType is substituted for whatever you tell it in <...> when making a Stack object
public class Stack<DataType> {
private List<DataType> memory = new ArrayList<>();
public void push(DataType e) {
memory.add(e);
}
public DataType pop() {
if(memory.isEmpty())
return null;
int lastIndex = memory.size()-1;
DataType element = memory.get(lastIndex); //get last element of memory
memory.remove(lastIndex); //remove it from the stack
return element; //return it
}
public boolean isEmpty() {
return memory.isEmpty();
}
}
//创建堆栈对象时,数据类型将替换您在中告诉它的任何内容
公共类堆栈{
私有列表内存=新的ArrayList();
公共无效推送(数据类型e){
添加(e);
}
公共数据类型pop(){
if(memory.isEmpty())
返回null;
int lastIndex=memory.size()-1;
DataType element=memory.get(lastIndex);//获取内存的最后一个元素
memory.remove(lastIndex);//将其从堆栈中删除
return元素;//返回它
}
公共布尔值为空(){
返回内存。isEmpty();
}
}
然后您可以像现在这样使用:
Stack<String> stack = new Stack<String>(); //DataType in Stack becomes String
Stack Stack=新堆栈()//堆栈中的数据类型变为字符串
您似乎误解了泛型的语法。在堆栈类中,泛型参数String
的行为更像一个变量,而不像String类
//DataType is substituted for whatever you tell it in <...> when making a Stack object
public class Stack<DataType> {
private List<DataType> memory = new ArrayList<>();
public void push(DataType e) {
memory.add(e);
}
public DataType pop() {
if(memory.isEmpty())
return null;
int lastIndex = memory.size()-1;
DataType element = memory.get(lastIndex); //get last element of memory
memory.remove(lastIndex); //remove it from the stack
return element; //return it
}
public boolean isEmpty() {
return memory.isEmpty();
}
}
//创建堆栈对象时,数据类型将替换您在中告诉它的任何内容
公共类堆栈{
私有列表内存=新的ArrayList();
公共无效推送(数据类型e){
添加(e);
}
公共数据类型pop(){
if(memory.isEmpty())
返回null;
int lastIndex=memory.size()-1;
DataType element=memory.get(lastIndex);//获取内存的最后一个元素
memory.remove(lastIndex);//将其从堆栈中删除
return元素;//返回它
}
公共布尔值为空(){
返回内存。isEmpty();
}
}
然后您可以像现在这样使用:
Stack<String> stack = new Stack<String>(); //DataType in Stack becomes String
Stack Stack=新堆栈()//堆栈中的数据类型变为字符串
堆栈从不“添加”或“删除”任何内容。它只设置或获取一个字符串字段,它不记得以前的值。你的empty
函数总是返回false。我是否需要向我的方法之一添加Arraylist来存储元素?或者这不是必需的?您确实需要使用某种形式的数组或列表,您将向堆栈中添加多个项,并且每个项都需要存储,实际上您实现的不是堆栈。您应该查看此链接,其中包括一些不同的堆栈实现—您的堆栈从不“添加”或“删除”任何内容。它只设置或获取一个字符串字段,它不记得以前的值。你的empty
函数总是返回false。我是否需要向我的方法之一添加Arraylist来存储元素?或者这不是必需的?您确实需要使用某种形式的数组或列表,您将向堆栈中添加多个项,并且每个项都需要存储,实际上您实现的不是堆栈。您应该查看这个链接,其中包括一些不同的堆栈实现是的,您是对的。有人建议我在尝试实现更通用的数据类型之前,先通过将类型定义为“String”来尝试泛型类。但似乎我的代码中的基本问题仍然存在。您的减号()
方法从未将e
设置为null
,因此empty()
始终为false,而while(!stack.empty())
永不终止。您对如何定义我的减号方法有什么建议吗?好了,我给您做了一个小堆栈。我没有尝试过,所以没有给出任何保证。它不一定是ArrayList,但您肯定需要某种数据结构来保存多个数据段。数组也可以(尽管如果数组已满,则必须调整其大小,这基本上会再次为您提供一个ArrayList)。你不需要在一个变量中存储很多东西(可能是一个很长的序列化对象字符串,但我们在这里变得非常深奥)是的,你是对的。有人建议我在尝试实现更通用的数据类型之前,先通过将类型定义为“String”来尝试泛型类。但似乎我的代码中的基本问题仍然存在。您的减号()
方法从未将e
设置为null
,因此empty()
始终为false,而while(!stack.empty())
永不终止。您对如何定义我的减号方法有什么建议吗?好了,我给您做了一个小堆栈。我没有试过,所以没有保证。它不一定是ArrayList,但你肯定需要一些