目录
一、问题描述
写了个实现单链表的代码,其中有一条按值删除,测试单链表插入int 1-1000,删除方法接受的参数是Object类型,但是发现删除的时候,只能删除到127。到128就石沉大海了。
下面是单链表结点数据类型:
public class SingleListNode {
private Object element;
private SingleListNode next;
public SingleListNode(Object it,SingleListNode nextval) {element = it;next = nextval;}
public SingleListNode(SingleListNode nextval) {next=nextval;}
public Object getElement() {return element;}
public void setElement(Object it) {this.element = it;}
public SingleListNode getNext() {return next;}
public void setNext(SingleListNode nextval) {this.next = nextval;}
}
单链表数据类型,里面有插入和删除的功能:
public class SLList implements LinkedList {
private SingleListNode head;
public SLList() {
head=new SingleListNode(null);
}
public void inserttoHead(Object item) {
// 将头结点的子节点设置为新节点的子节点
// 将新节点作为头结点的子节点
head.setNext(new SingleListNode(item,head.getNext()));
}
public void removebyValue(Object item) {
// System.out.println(item);
/**
* 用一个游标来记录当前节点的父节点
*/
if(isEmpty()==true)return;
SingleListNode curNode = head.getNext();
SingleListNode curNode_father = head;
if((int)item>127)System.out.println(curNode.getElement());
/**
* 当传入的item是int,且>127时,如128,我们发现即使curNode.getElement()的值是128那也不会被判定为相等
*
*/
while((curNode!=null)&&(curNode.getElement()!=item)) {
if((int)item>127) {System.out.println(curNode.getElement());}
curNode_father = curNode;
curNode = curNode.getNext();
}
if((curNode!=null)&&(curNode.getElement()==item)) {
curNode_father.setNext(curNode.getNext());
System.out.println(item);
}
}
}
测试代码:
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
SLList sll = new SLList();
long startnsTime = System.nanoTime();//精确到纳秒
for(int i=1000;i>0;i--) {
sll.inserttoHead(i); //单链表顺序 1、。。。、1000
}
long endnsTime = System.nanoTime();//精确到纳秒
System.out.println("插入到头1~1000时间:"+(endnsTime-startnsTime)+"ns");
int i=0;
long startTime_removebyvalue_allfromhead = System.nanoTime();
while((i++)<1001) {
sll.removebyValue(i);// 删除顺序 1、...、1000
}
long endTime_removebyvalue_allfromhead = System.nanoTime();
System.out.println("按值从头删除:"+(endTime_removebyvalue_allfromhead-startTime_removebyvalue_allfromhead)+"ns");}
}
运行过程中发现只能删除到127,从128开始无法删除,经过测试,是从removebyValue方法中的删除代码的这段命令开始变化:
curNode.getElement()!=item
就是超过127的int值不能与被赋予同样int值的Object再等值了。
二、问题解析
int是一种基本数据类型,是原语。不会作为对象来处理。
public class test {
public static void main(String[] args) {
Object it = new Object(); // Object类型
int i