根据视频整理
class Link {
class Node {
private Node next;// 下一个节点
private Object data;// 数据
public Node(Object data) {
this.data = data;
}
// 1.添加节点
public void addNode(Node newNode) {
if (this.next == null) {
this.next = newNode;
} else {
this.next.addNode(newNode);
}
}
// 2.数据查询
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;
}
}
}
// 3.修改数据
public void setNode(int index, Object data) {
if (Link.this.foot++ == index) {
this.data = data;
}
this.next.setNode(index, data);
}
// 4.获取数据
public Object getData(int index) {
if (Link.this.foot++ == index) {
return this.data;
}
return this.next.getData(index);
}
// 5.删除数据
public void removeNode(Node previous, Object data) {
if (data.equals(this.data)) {
previous.next = this.next;
} else {
this.next.removeNode(this, data);
}
}
// 6.对象数组
public void toArrayNode() {
Link.this.retArray[Link.this.foot++] = this.data;
if (this.next != null) {
this.next.toArrayNode();
}
}
}
// ===================以上是内部类=========================
private Node root;// 根节点
private int count;// 计数器
private int foot;// 脚标
private Object[] retArray;// 对象数组
// 1.添加数据
public void add(Object data) {
if (data == null) {
return;
}
Node newNode = new Node(data);// 数据装包
if (this.root == null) {
this.root = newNode;
} else {
this.root.addNode(newNode);
}
this.count++;
}
// 2.链表长度
public int size() {
return this.count;
}
// 3.链表是否为空
public boolean isEmpty() {
return this.count == 0;
}
// 4.链表查询
public boolean contains(Object data) {
if (data == null || this.root == null) {
return false;
}
return this.root.containsNode(data);
}
// 5.修改数据
public void set(int index, Object data) {
if (index > this.count) {
return;
}
this.foot = 0;
this.root.setNode(index, data);
}
// 6.获取数据
public Object get(int index) {
if (index > this.count) {
return null;
}
this.foot = 0;
return this.root.getData(index);
}
// 7.删除数据
public void remove(Object data) {
if (this.contains(data)) {
if (data.equals(this.root.data)) {
this.root = this.root.next;
} else {
this.root.next.removeNode(this.root, data);
}
}
this.count--;
}
// 8.对象数组
public Object[] toArray() {
if (this.root == null) {
return null;
}
this.retArray = new Object[this.count];
this.root.toArrayNode();
return this.retArray;
}
}
interface Pet{//定义宠物接口
public String getName();//定义方法 取得名字信息
public int getAge();//定义方法 取得年龄信息
}
class petShop {//定义宠物商店
private Link pets = new Link();//链表保存宠物信息
public void add(Pet pet) {
this.pets.add(pet);//向链表保存数据
}
public void delete(Pet pet) {
this.pets.remove(pet);//从链表中删除宠物信息
}
public Link search(String keyWord) {
Link result = new Link();//保存结果
//将结果合变为对象数组的形式返回,因为集合保存的是Objective
//但是真正要查询的数据在pet接口对象的getName()方法的返回值
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;
public Cat(String name,int age){
this.name = name;
this.age = age;
}
public boolean equals(Object obj){
if (this == obj){
return true;
}
if (this == 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 toString(){//覆写Objective类中的toString
return "猫的名字:"+this.name+"年龄:"+this.age;
}
}
class Dog implements Pet{
private String name;
private int age;
public Dog(String name,int age){
this.name = name;
this.age = age;
}
public boolean equals(Object obj){
if (this == obj){
return true;
}
if (this == null){
return false;
}
if (!(obj instanceof Dog)){
return false;
}
Dog d = (Dog) obj;
if(this.name.equals(d.name)&&this.age == d.age){
return true;
}
return false;
}
public String getName(){//覆写
return this.name;
}
public int getAge(){
return this.age;
}
public String toString(){//覆写Objective类中的toString
return "狗的名字:"+this.name+"年龄:"+this.age;
}
}
public class Main {
public static void main(String[] args) {
petShop shop = new petShop();
shop.add(new Cat("一号猫", 1));
shop.add(new Cat("二号猫", 2));
shop.add(new Cat("三号猫", 3));
shop.add(new Cat("四号猫", 4));
shop.add(new Dog("一号狗", 1));
shop.add(new Dog("二号狗", 1));
shop.add(new Dog("三号狗", 1));
shop.add(new Dog("四号狗", 1));
Link all = shop.search("");//输入查询的关键字
Object obj[] = all.toArray();
for (int x = 0; x < obj.length; x++) {
System.out.println(obj[x]);
}
}
}
运行结果:
基本的接口思路,不难,但是很重要