컬렉션 프레임워크(Collection은 학생 객체를 저장하고 이동)

1719 단어
핵심 코드 블록:
package cn.ithelei;

public class Student {
//  
private String name;
private int age;

//  
public Student() {
    super();
}

public Student(String name, int age) {
    super();
    this.name = name;
    this.age = age;
}


//  
// getXxx()/setXxx()
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

@Override
public String toString() {
    return "Student [name=" + name + ", age=" + age + "]";
    }
}

//
 package cn.ithelei;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 
 * @Package cn.ithelei
 * @ClassName: CollectionTest
 * @Description: TODO( )
 * @author Administrator
 * @date 2018-4-6  11:19:45
 * @version 1.0
 */

/**
 *  
 *  
 *  
 *  
 *  
 * 
 */
public class CollectionTest {
public static void main(String[] args) {
    // 
    Collection c=new ArrayList();
    
    // 
    
    Student s=new Student("helei",23);
    Student s1=new Student("hetao",20);
    Student s2=new Student();
    s2.setName("heqiang");
    s2.setAge(18);
    
    
    // 
    c.add(s);
    c.add(s1);
    c.add(s2);
    c.add(new Student("jianfeng",18));// 
    
     
    // 
    Iterator iterator =c.iterator();
    
    while (iterator.hasNext()) {
        Student student = (Student) iterator.next();
        
        System.out.println(student.getName()+student.getAge());
        
    }
    
    
   }

}

4
  • 메일박스:[email protected]

  • 4
  • 기술 토론군: 687856230
  • GoodLuck
  • 좋은 웹페이지 즐겨찾기