为我自己的类对象制作我自己的 FIFO 队列类来填充它吗?
我正在尝试创建一个 FIFO 队列,其中充满了我自己的类对象。
我找到了这个例子,但如果我替换 < E>与 <印刷电路板>它不起作用:
import java.util.LinkedList;
public class SimpleQueue<E> {
private LinkedList<E> list = new LinkedList<E>();
public void put(E o) {
list.addLast(o);
}
public E get() {
if (list.isEmpty()) {
return null;
}
return list.removeFirst();
}
public Object[] getAll() {
Object[] res = new Object[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
list.clear();
return res;
}
public E peek() {
return list.getFirst();
}
public boolean isEmpty() {
return list.isEmpty();
}
public int size() {
return list.size();
}
}
I am trying to make a FIFO Queue that is filled with my own class object.
I found this example but if I replace < E > with < PCB > it does not work:
import java.util.LinkedList;
public class SimpleQueue<E> {
private LinkedList<E> list = new LinkedList<E>();
public void put(E o) {
list.addLast(o);
}
public E get() {
if (list.isEmpty()) {
return null;
}
return list.removeFirst();
}
public Object[] getAll() {
Object[] res = new Object[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
list.clear();
return res;
}
public E peek() {
return list.getFirst();
}
public boolean isEmpty() {
return list.isEmpty();
}
public int size() {
return list.size();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
E
是一个类型参数。简而言之,您可以将其视为一个“模板”,可用于创建一个可以保存一个特定类的实例的队列。您可以按如下方式创建
PCB
对象队列:Java 泛型常见问题解答<如果您想了解有关 Java 泛型的更多信息,/a> 是一个很好的资源。
E
is a type parameter. In simple terms, you can consider it as a 'template' which can be used to create a queue that can hold instances of one particular class.You can create a queue of your
PCB
objects as follows:Java Generics FAQs is a good resource if you want to learn more about Java generics.
太阳的通用教程如下:
所以,不可能是你改成
PCB
的问题。但是,如果 PCB 是您想要存储对象的唯一类,则不必创建通用类。只需从类定义行中删除
并将所有E
替换为PCB
:The sun's generic tutorial says following:
So, it can't be the problem that you changed it to
PCB
.But if
PCB
is the only class of which you want to store objects, you don't have to create a generic class. Just remove<PCB>
from your class definition line and replace allE
's withPCB
: