有大佬知道[face]qq:54.gif這是哪錯了

鏈表的代碼
class Link {
private class Node {
private Object data;
private Node next ;
public Node ( Object data ) {
this.data = data ;
}
public void addNode (Node newNode){
if ( this.next == null ) {
this.next = newNode ;
}else {
this.next.addNode(newNode);
}
}
public void print () {
System.out.println(this.data + "\t");
if(this.next != null ) {
this.next.print();
}
}
public boolean containsNode(Object data) {
if(data.equals(this.data)) {
return true;
}else {
if(this.next!=null) {
return this.next.containsNode(data);
}else {
return false;
}
}
}
public Object getNode(int index) {
if(Link.this.foot ++==index) {
return this.data;
}else {
return this.next.getNode(index);
}
}
public void delete(Node previous,Object data) {
if(data.equals(this.data)) {
previous.next=this.next;
}else {
if(this.next!=null) {
this.next.delete(this, data);
}
}
}
public void toArrayNode() {
Link.this.retArray[Link.this.foot++]=this.data;
if(this.next!=null) {
this.next.toArrayNode();
}
}
}
private Node root ;
private int foot=0;
private int count=0;
private Object[] retArray;
public void add ( Object data ) {//增加鏈表
Node newNode = new Node (data) ;
if ( this.root == null ) {
this.root = newNode ;
} else {
this.root.addNode (newNode) ;
}
this.count++;
}
public void printNode () {//輸出
if ( this.root != null ) {
this.root.print();
}
}
public int size() {
return this.count;
}
public Object get(int index) {
if(index>this.count) {
return null;
}
this.foot=0;
return this.root.getNode(index);
}
public boolean contains(Object data) {
if(data=https://bbs.csdn.net/topics/=null ||this.root==null) {
return false;
}
return this.root.containsNode(data);
}
public void deleteNode (Object data) {
if(this.contains(data)) {
if(this.root.data.equals(data)) {
this.root=this.root.next;
}else {
this.root.next.delete(root,data);
}
}
}
public Object [] toArray(){
if(this.root==null) {
return null;
}
this.foot=0;
this.retArray=new Object [this.count];
this.root.toArrayNode();
return this.retArray;
}
}
寵物介面
interface Pet {
public String getName();
public int getAge();
public String getColor();
}
寵物商店
class Petshop {
private Link pets = new Link();
public void add(Pet pet){
this.pets.add(pet);
}
public void deleteNode(Pet pet) {
this.pets.deleteNode(pet);
}
public void print() {
this.pets.printNode();
}
public Link search(String keyWord) {
Link result = new Link();
Object obj [] = this.pets.toArray();
for(int x = 0;x<obj.length;x++) {
Pet p=(Pet) obj[x];
if(p.getName().contains(keyWord)) {
result.add(p);
}
}
return result ;
}
}
貓咪子類
class Cat implements Pet{
private String name ;
private int age;
private String color;
public Cat(String name,int age,String color) {
this.name=name ;
this.age=age ;
this.color=color ;
}
public boolean equals(Object obj) {
if(this==obj) {
return true ;
}
if(obj==null) {
return false;
}if(!(obj instanceof Cat)) {
return false;
}
Cat c = (Cat) obj;
if(this.name.equals(c.name) && this.age==c.age ) {
return true;
}
return false;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String getColor() {
return this.color;
}
public String toString() {
return "姓名:"+this.name+"年齡:"+this.age+"顏色:"+this.color;
}
}
主函式
public class Shop {
public static void main(String[] args) {
Petshop shop = new Petshop();
shop.add(new Cat("黑貓",2,"黑色的"));
shop.add(new Cat("白貓",2,"白色的"));
shop.add(new Cat("橘貓",2,"黃色的"));
shop.deleteNode(new Cat("橘貓",2,"黃色的"));
shop.print();
Link all = shop.search("黑");
Object obj[]=all.toArray();
for(int x=0;x<obj.length;x++) {
System.out.println(obj[x]);
}
}
}

uj5u.com熱心網友回復:
因為你洗掉node時沒減count值,造成object[]有空值。
public void deleteNode(Object data) {
if (this.contains(data)) {
if (this.root.data.equals(data)) {
this.root = this.root.next;
} else {
this.root.next.delete(root, data);
}
this.count--;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/100465.html
標籤:Eclipse
上一篇:select標簽不顯示
