자바 의 Collection 사용 사례[첫 번 째 저장 문자열 두 번 째 저장 학생 클래스]

  • 사례 1:문자열 저장
    /*
    * 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());       //      
    
    
        }
    
    }
    
  • 사례 2:학생 정보 저장
    //   
    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());
        }
    }
    
  • 좋은 웹페이지 즐겨찾기