java链表包装类
我正在编写一个方法,该方法假定查找并返回链表中元素的值,链表显式定义为保存布尔值(包装器类) 代码如下:java链表包装类,java,Java,我正在编写一个方法,该方法假定查找并返回链表中元素的值,链表显式定义为保存布尔值(包装器类) 代码如下: public boolean lookup(int index) { LLNode<Boolean> nodeptr = getFirst(); for(int i = 0; i < index; i ++){ if(nodeptr == null) return false; // ?????? nodeptr = nodeptr.g
public boolean lookup(int index) {
LLNode<Boolean> nodeptr = getFirst();
for(int i = 0; i < index; i ++){
if(nodeptr == null)
return false; // ??????
nodeptr = nodeptr.getNext();
}
return nodeptr.getElement();
}
我觉得这很奇怪,因为我假设Java 7应该自动打开我的包装类,所以我尝试:
public boolean lookup(int index) {
LLNode<Boolean> nodeptr = getFirst();
for(int i = 0; i < index; i ++){
if(nodeptr == null)
return false; // ??????
nodeptr = nodeptr.getNext();
}
boolean b = nodeptr.getElement().booleanValue();
return b;
}
我不明白这一点,因为我在lang包中显式地从API及其应用程序中查找并复制/粘贴了这个方法,所以它应该自动导入。当我手动导入此包时,仍然出现相同的错误
有什么建议吗
我被要求发布LLNode类:
public class LLNode<T> {
private LLNode<T> next;
private LLNode<T> last;
private T element;
public LLNode(T element, LLNode<T> next, LLNode<T> last) {
this.element = element;
this.next = next;
this.last = last;
}
private void setElement(T element) {
this.element = element;
}
public T getElement() {
return element;
}
public LLNode<T> getNext(){
return next;
}
public LLNode<T> getLast() {
return last;
}
public void setNext(LLNode<T> next) {
this.next = next;
}
public void setLast(LLNode<T> last) {
this.last = last;
}
}
公共类LLNode{
私有LLNode-next;
私有LLNode-last;
私有T元素;
公共LLNode(T元素,LLNode next,LLNode last){
this.element=元素;
this.next=next;
this.last=last;
}
私有void集合元素(T元素){
this.element=元素;
}
公共T getElement(){
返回元素;
}
公共LLNode getNext(){
下一步返回;
}
公共LLNode getLast(){
最后返回;
}
public void setNext(LLNode next){
this.next=next;
}
公共void setLast(LLNode last){
this.last=last;
}
}
getFirst()方法:
受保护的LLNode getFirst(){
先返回;
}
将查找方法更改为:
public boolean lookup(int index) {
LLNode<Boolean> nodeptr = getFirst();
for(int i = 0; i < index; i ++){
if(nodeptr == null)
return false; // ??????
nodeptr = nodeptr.getNext();
}
Object element = nodeptr.getElement();
if( element != null && element instanceof Boolean )
{
return ((Boolean)element).booleanValue();
}
return false;
}
公共布尔查找(int索引){
LLNode nodeptr=getFirst();
对于(int i=0;i
能否显示LLNode的代码?能否显示LLNode
中的相关代码?问题可能就在这里。你不能简单地将查找的返回类型改为布尔值吗?我希望索引查找是get(index)
,它会在索引处返回布尔值,或者返回索引自动边界异常。你能给我们看一下getFirst()
方法?
public class LLNode<T> {
private LLNode<T> next;
private LLNode<T> last;
private T element;
public LLNode(T element, LLNode<T> next, LLNode<T> last) {
this.element = element;
this.next = next;
this.last = last;
}
private void setElement(T element) {
this.element = element;
}
public T getElement() {
return element;
}
public LLNode<T> getNext(){
return next;
}
public LLNode<T> getLast() {
return last;
}
public void setNext(LLNode<T> next) {
this.next = next;
}
public void setLast(LLNode<T> last) {
this.last = last;
}
}
protected LLNode<Boolean> getFirst() {
return first;
}
public boolean lookup(int index) {
LLNode<Boolean> nodeptr = getFirst();
for(int i = 0; i < index; i ++){
if(nodeptr == null)
return false; // ??????
nodeptr = nodeptr.getNext();
}
Object element = nodeptr.getElement();
if( element != null && element instanceof Boolean )
{
return ((Boolean)element).booleanValue();
}
return false;
}