Home > Back-end >  Have bosses know what is wrong with the list made a pet shop
Have bosses know what is wrong with the list made a pet shop

Time:10-01

There are bosses know [face] qq: 54. GIF what wrong it is
List the code
The class Link {
Private class Node {
Private Object data;
Private Node next;
Public Node (Object data) {
This. Data=https://bbs.csdn.net/topics/data;
}
Public void invoke the (Node newNode) {
If (this. Next==null) {
This. Next=newNode;
} else {
This. Next. Invoke the (newNode);
}
}
Public void print () {
System. The out. Println (enclosing data + "\ t");
If (this. Next!=null) {
This. Next. Print ();
}
}
Public Boolean containsNode (Object data) {
If (data) equals (enclosing 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. The data;
} else {
Return this. Next. GetNode (index);
}
}
Public void the delete (Node previous, Object data) {
If (data) equals (enclosing data)) {
Previous. Next=this. Next;
} else {
If (this. Next!=null) {
This. Next. Delete (this, data);
}
}
}

Public void toArrayNode () {
The 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 the add (Object data) {//increase list
The Node newNode=new Node (data);
If (this. Root==null) {
This. The root=newNode;
} else {
This. Root. Invoke the (newNode);
}
This. Count++;
}
Public void printNode () {//output
If (this. Root!=null) {
This. The root. The print ();
}
}
Public int size () {
Return this. Count;
}
Public Object get (int index) {
If (index> This. Count) {
return null;
}
This. The foot=0;
Return this. The root. GetNode (index);
}
Public Boolean contains (Object data) {
If (data=https://bbs.csdn.net/topics/=null | | this. Root==null) {
return false;
}
Return this. The root. ContainsNode (data);
}
Public void deleteNode (Object data) {
If (this. The contains (data)) {
If (this. Root. Data. Equals (data)) {
Enclosing root=this. Root. Next;
} else {
Enclosing root. Next. Delete (root, data);
}
}
}
Public Object [] toArray () {
If (this. Root==null) {
return null;
}
This. The foot=0;
Enclosing retArray=new Object [this count];
This. Root. ToArrayNode ();
Return this. RetArray;
}

}


Pet interface



Interface Pet {
Public String getName ();
Public int getAge ();
Public String getColor ();

}





Pet shop

The class Petshop {
Private Link pets=new Link ();
Public void the 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 the result=new Link ();
The Object obj []=this. Pets. ToArray ();
For (int x=0; xPet p=(Pet) obj [x];

If (p.g etName (). The contains (keyWord)) {
Result. The add (p);

}
}

Return the result.
}
}

Cat subclass
The class implements Pet Cat {
Private String name;
Private int the age;
Private String color;
Public Cat (String name, int the age, the String color) {
This. Name=name;
Enclosing the 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;
}
C=(Cat) of the Cat obj;
If (this. Name. Equals (ame) & amp; & Enclosing the age==c.a. ge) {
return true;
}
return false;
}
Public String getName () {
Return this. The name;
}
Public int getAge () {
Return this. The age;
}
Public String getColor () {
Return this. The color;
}


Public String toString () {
Return "name:" + this. The name + "age:" + this. Age + "color:" + this. Color;
}
}


The main function
Public class Shop {
Public static void main (String [] args) {
Petshop shop=new Petshop ();
Shop. The add (new Cat (" black Cat ", 2, "black"));
Shop. The add (new Cat (" white Cat ", 2, "white"));
Shop. The add (new Cat (" orange Cat ", 2, "yellow"));
Shop. DeleteNode (new Cat (" orange Cat ", 2, "yellow"));
Shop. Print ();
The Link all=shop. The search (" black ");
The Object obj []=all. ToArray ();
For (int x=0; xSystem. The out. Println (obj [x]).
}



}
}

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related