자바 의 Collection 사용 사례[첫 번 째 저장 문자열 두 번 째 저장 학생 클래스]
/*
* Collection     
* 1.     
* 2.     
* 3.     
* 4.   
* */
public class TestCollection {
       
    public static void main(String[] args) {
       
        //    
        Collection cn=new ArrayList();
        System.out.println("-------------------1.    ---------------------");
        //1.    
        cn.add("  ");
        cn.add("  ");
        cn.add("    ");
        cn.add("  ");
        cn.add("   ");
        cn.add("  ");
        System.out.println("      :"+cn.size());
        System.out.println(cn);
        System.out.println("-------------------2.    ---------------------");
        //2.    
//        cn.remove("    ");
//        System.out.println(cn);
//        cn.clear();
//        System.out.println("      :"+cn.size());
        System.out.println("-------------------3.    ---------------------");
        //3.    
        // (1)     for    
        for (Object o : cn) {
       
            System.out.println(o);
        }
        // (2)        (                )
        // hasNext();          。
        // next();         
        // remove();        
        Iterator it=cn.iterator();
        while(it.hasNext()){
       
            String s=(String)it.next();
            System.out.println(s);
            //cn.remove(s);               Collection           
            it.remove();
        }
        System.out.println("      :"+cn.size());
        //4.   
        System.out.println(cn.contains("  "));  //       
        System.out.println(cn.isEmpty());       //      
    }
}
//   
public class Student {
       
    private String name;
    private int age;
    public String getName() {
       
        return name;
    }
    public int getAge() {
       
        return age;
    }
    public void setName(String name) {
       
        this.name = name;
    }
    public void setAge(int age) {
       
        this.age = age;
    }
    public Student(String name, int age) {
       
        this.name = name;
        this.age = age;
    }
    public Student() {
       
    }
    @Override
    public String toString() {
       
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class TestCollection1 {
       
    public static void main(String[] args) {
       
        //  Collection  
        Collection co=new ArrayList();
        Student s1 = new Student("  ",12);
        Student s2 = new Student("  ",12);
        Student s3 = new Student("  ",12);
        //1.    
        co.add(s1);
        co.add(s2);
        co.add(s3);
        System.out.println("      "+co.size());
        System.out.println(co.toString());
        //2.  
        co.remove(s1);
        //co.remove(new Student("  ",12));     
        //co.clear();
        System.out.println(co.toString());
        System.out.println("      "+co.size());
        //3.  
        System.out.println("------------  for  -------------");
        //  for  
        for (Object o : co) {
       
            Student s=(Student)o;
            System.out.println(s.toString());
        }
        System.out.println("------------   -------------");
        //    
        Iterator it=co.iterator();
        while (it.hasNext()){
       
            Student s=(Student)it.next();
            System.out.println(s.toString());
        }
        //4.  
        System.out.println(co.contains(new Student("  ",12)));
        System.out.println(co.isEmpty());
    }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Dubbo (2): zookeeper 등록 센터Zookeeper 는 Apacahe Hadoop 의 하위 프로젝트 로 트 리 형태의 디 렉 터 리 서비스 로 푸 시 변경 을 지원 하 며 Dubbo 서비스의 등록 센터 로 적합 하 며 산업 강도 가 높 아 생산 환경...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.