1、使用类集的好处:可以更改对象数组的大小(弥补了数组不能更改数据长度这一缺陷),比链表使用起来简单。
2、类集接口基于:import java.util.*;
3、类集框架借口分类:List结构集合类(ArrayList、LinkList、Vector、Stack)、map结构集合类(Hashmap、Hashtable)、Set结构集合(Hashset(无序存放,自动覆盖重复的)、Treeset(有序存放,自动覆盖重复的))。
4、需要线程安全时使用:vector(数据量大时优先考虑)、Hastable
不需要线程安全时用:ArrayList、LinkList、Hashmap
需要键值时用:Hashmap、Hashtable
5、代码1:package com.cqvie.Vector;
import java.util.*;
public class TestDemo {
public static void main(String[]args)throws Exception{
List<String> all=new Vector<String>();
all.add("hello");
all.add("hello");
all.add("world");
for(int i=0;i<all.size();i++){
String str= all.get(i);
System.out.print(str+"、");
}
}
}
代码2:
package com.cqvie.yy.demo;
import java.util.*;
public class TestDemo {
public static void main(String[] args) {
List<String>all=new ArrayList<String>();//实例化接口
all.add("Hello");
all.add("hello");
all.add("world");
all.remove("hello");
for(int i=0;i<all.size();i++){
String str=all.get(i);
System.out.print(str+"、");
}
}
}
代码3:package com.cqvie.yy.TreeSet;
import java.util.*;
public class Preson implements Comparable<Preson>{
private String name;
private int age;
public Preson(String name,int age){
this.name=name;
this.age=age;
}
@Override
public int compareTo(Preson arg0) {
if(this.age> arg0.age) return 1;
return-1;
}
public String toString(){
return "姓名:"+this.name+" 年龄:"+this.age+"\n";
}
}
package com.cqvie.yy.TreeSet;
import java.util.*;
public class TsetDemo {
public static void main(String[] args)throws Exception {
Set<Preson> all=new TreeSet<Preson>();
all.add(new Preson("张三",22));
all.add(new Preson("王五",18));
all.add(new Preson("李四",22));
all.add(new Preson("赵强",29));
all.add(new Preson("王麻子",20));
System.out.println(all);
}
}
代码4:package com.cqvie.yy.Demo1;
import java.util.*;
interface Animal{
public String getName();
public int getAge();
}
public class Zoo {
private List<Animal> animals=new ArrayList<Animal>();
public void add(Animal a){
this.animals.add(a);
}
public void delete(Animal a){
this.animals.remove(a);
}
public List<Animal> search(String name){
List<Animal> result=new ArrayList<Animal>();
Object obj[]=this.animals.toArray();
for(int i=0;i<obj.length;i++){
Animal a=(Animal) obj[i];
if(a.getName().contains(name)){
result.add(a);
}
}
return result;
}
public List<Animal> getAnimals() {
return animals;
}
public void setAnimals(List<Animal> animals) {
this.animals = animals;
}
}
package com.cqvie.yy.Demo1;
public 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 aquals(Object obj){
if(this==obj){
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof Dog)){
return false;
}
Tiger tiger =(Tiger) obj;
if(this.name.equals(tiger.name)&&this.age==tiger.age){
return true;
}
return false;
}
public String toString(){
return"名字"+this.name+" 年龄"+this.age;
}
}
package com.cqvie.yy.Demo1;
public 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 aquals(Object obj){
if(this==obj){
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof Dog)){
return false;
}
Dog dog1 =(Dog) obj;
if(this.name.equals(dog1.name)&&this.age==dog1.age){
return true;
}
return false;
}
public String toString(){
return"名字"+this.name+" 年龄"+this.age;
}
}
package com.cqvie.yy.Demo1;
import java.util.*;
public class TestDemo {
public static void main(String[] args) {
Zoo zoo=new Zoo();
zoo.add(new Dog("花狗",1));
zoo.add(new Dog("黄狗",2));
zoo.add(new Dog("黑狗",3));
zoo.add(new Dog("斑点狗",1));
zoo.add(new Tiger("斑点虎",2));
zoo.add(new Tiger("黑虎",2));
//zoo.delete(aaaa);
List<Animal> result=zoo.search("黑");
Object obj[]=result.toArray();
for (int x=0;x<obj.length;x++){
System.out.println(obj[x]);
}
}
private static Animal Dog(String string, int i) {
// TODO Auto-generated method stub
return null;
}
}