前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >list集合示例代码

list集合示例代码

作者头像
葆宁
发布2019-04-18 17:07:35
6420
发布2019-04-18 17:07:35
举报
文章被收录于专栏:FREE SOLOFREE SOLO
代码语言:javascript
复制
import java.util.ArrayList;
import java.util.List;

interface Animal {	// 动物
    public String getName() ;
    public int getAge() ;
}
class Zoo {
    private List<Animal> animals = new ArrayList<Animal>() ;	// 多个动物
    public void add(Animal ani) {	// 增加动物
        this.animals.add(ani) ;	// 增加动物
    }
    public void delete(Animal ani) {
        this.animals.remove(ani) ;	// 需要equals()
    }
    public List<Animal> search(String keyWord) {
        List<Animal> result = new ArrayList<Animal>() ;
        for (int x = 0 ; x < this.animals.size() ; x ++) {
            Animal ani = this.animals.get(x) ;
            if (ani.getName().contains(keyWord)) {	// 满足
                result.add(ani) ;
            }
        }
        return result ;
    }
}
class Dog implements Animal {
    private String name ;
    private int age ;
    public Dog(String name,int age) {
        this.name = name ;
        this.age = age ;
    }
    public String getName() {
        return this.name ;
    }
    public int getAge() {
        return this.age ;
    }
    public boolean equals(Object obj) {
        if (this == obj) {
            return true ;
        }
        if (obj == null) {
            return false ;
        }
        if (!(obj instanceof Dog)) {
            return false ;
        }
        Dog dog = (Dog) obj ;
        if (this.name.equals(dog.name)
                && this.age == dog.age) {
            return true ;
        }
        return false ;
    }
    public String toString() {
        return "〖狗的信息〗名字:" + this.name + ",年龄:" + this.age ;
    }
}
class Tiger implements Animal {
    private String name ;
    private int age ;
    public Tiger(String name,int age) {
        this.name = name ;
        this.age = age ;
    }
    public String getName() {
        return this.name ;
    }
    public int getAge() {
        return this.age ;
    }
    public boolean equals(Object obj) {
        if (this == obj) {
            return true ;
        }
        if (obj == null) {
            return false ;
        }
        if (!(obj instanceof Tiger)) {
            return false ;
        }
        Tiger t = (Tiger) obj ;
        if (this.name.equals(t.name)
                && this.age == t.age) {
            return true ;
        }
        return false ;
    }
    public String toString() {
        return "〖老虎的信息〗名字:" + this.name + ",年龄:" + this.age ;
    }
}
public class Demo01 {
    public static void main(String args[]) {
        Zoo zoo = new Zoo();    // 动物园
        zoo.add(new Dog("花狗", 1));
        zoo.add(new Dog("黄狗", 1));
        zoo.add(new Dog("黑狗", 1));
        zoo.add(new Dog("斑点狗", 1));
        zoo.add(new Tiger("斑点虎", 2));
        zoo.add(new Tiger("黑虎", 2));
        zoo.add(new Tiger("花虎", 2));
        zoo.delete(new Dog("斑点狗", 1));    // 删除
        List<Animal> result = zoo.search("斑点");
        for (int x = 0; x < result.size(); x++) {
            System.out.println(result.get(x));
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年03月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档