java - 链表的通用方法
问题描述
试图让这两种方法起作用,但我对泛型没有太多经验,而且这个概念很混乱。
任何 getFirst():返回存储在列表中第一个节点中的值。如果列表为空,它应该打印一条错误消息并返回 null。
任何 getLast():返回存储在列表中最后一个节点中的值。如果列表为空,它应该打印一条错误消息并返回 null。
这是我的代码:(上面的方法出现在底部)
public class Node<Anything>{
private Anything data;
private Node next;
Node(Anything a, Node n)
{
data = a;
next = n;
}
public Anything getData()
{
return this.data;
}
public Anything setData(Anything newData)
{
Anything oldData = this.data;
this.data = newData;
return oldData;
}
public void setNext(Node newNext)
{
this.next = newNext;
}
public Node getNext()
{
return this.next;
}
}
-----------------------------------------------
public class CS2LinkedList<Anything>{
private Node first;
private Node last;
public CS2LinkedList()
{
first = null;
}
public boolean isEmpty()
{
return (first == null);
}
public void addFirst(Anything d)
{
Node temp = first;
first = new Node(d,temp);
}
public void clear()
{
first = null;
}
public boolean contains(Anything value)
{
for (Node curr = first; curr != null; curr = curr.getNext())
{
if (value.equals(curr.getData())){
return true;
}
}
return false;
}
public String toString()
{
StringBuilder result = new StringBuilder(); //String result = "";
for (Node curr = first; curr != null; curr = curr.getNext())
result.append(curr.getData() + "->"); //result = result + curr.data + "->";
result.append("[null]");
return result.toString(); //return result + "[null]";
}
public int size()
{
int size = 0;
for (Node curr = first; curr != null; curr = curr.getNext()){
size++;
if (first==null){
size = 0;
}
}
return size;
}
// ------------------------ Question begins here ------------------------
public Anything getFirst()
{
if (first != null){
// What should I return here? I tried returning first, (Anything) first, but none of them seems to work.
}
else{
return null;
}
}
public Anything getLast()
{
if (first != null){
// Same here
}
else{
return null;
}
}
解决方案
该类Node
有一个类型参数,但在您的类中,CS2LinkedList
您使用它时没有类型参数。您将其用作原始类型。原始类型仅存在于 Java 中是为了向后兼容没有泛型的非常旧的 Java 版本。您不应该使用原始类型(除非绝对必要,因为您必须使用非常旧的代码)。
Node
你在课堂CS2LinkedList
上写作的任何地方,都改写Node<Anything>
。例如,像这样声明成员变量:
private Node<Anything> first;
private Node<Anything> last;
像这样写你的addFirst
方法:
public void addFirst(Anything d)
{
Node<Anything> temp = first;
first = new Node<>(d,temp);
}
等等。
然后,您可以getFirst()
像这样编写您的方法:
public Anything getFirst()
{
if (first != null){
return first.getData();
}
else{
return null;
}
}
方法类似getLast()
。
您还需要修改类中的一些代码Node
。构造函数参数和setNext
方法的参数也应该有一个类型参数:
Node(Anything a, Node<Anything> n)
{
data = a;
next = n;
}
public void setNext(Node<Anything> newNext)
{
this.next = newNext;
}
以及方法的返回类型getNext
:
public Node<Anything> getNext()
{
return this.next;
}
推荐阅读
- javascript - 重复分组状态并绑定到 onChange 事件
- android - 在工厂中放置什么来创建卡片视图 - recyclerviews、适配器
- android - 当服务作为不同进程运行时,SharedPreference 与服务不同步
- oracle - Oracle Text catsearch 左截断搜索
- dart - 在 dart 中使用 'as' 进行类型转换
- laravel - Foreach 在我的 laravel 5.6 视图中不起作用
- arrays - 遍历 Julia 中的多个变量(或整个矩阵)
- javascript - setTimeout 在我的新 Kendo MVC 项目中不起作用
- html - 鼠标悬停在文本上时如何添加帮助文本?
- angular - 如何在不破坏组件angular5的情况下更改url