jdk 1.6LinkedList+iterator 举例

package com.itcast.practice;

import java.util.Iterator;
import java.util.LinkedList;

public class test1
 {
  /**
   * 1、 创建一个类Sample,在main方法中定义一个ArrayList的集合类, 并向集合中添加任意多个同一种类型的元素,
   * 利用Iterator接口,遍历集合中的所有元素。
   */

  public static void main(String[] args) {
   EnumTest ee = new EnumTest("fuyuan", true, 24);
   EnumTest zz = new EnumTest("zhangsan", false, 19);
   // classcastexception 异常代码
   // List listtestList = (List) new LinkedList();
   LinkedList listtestList = new LinkedList();
   listtestList.add(ee);
   listtestList.add(zz);
   Iterator iterator = ((java.util.List) listtestList).iterator();
   while (iterator.hasNext()) {
    EnumTest enumTest = (EnumTest) iterator.next();
    System.out.println(enumTest.getNameString() + enumTest.getYear());
   }
  }
 }

 

================================================================

package com.itcast.practice;

public class EnumTest
 {
  private String nameString;
  private boolean sex;
  private int  year;

  /**
   * @param nameString
   * @param sex
   * @param year
   */
  public EnumTest(String nameString, boolean sex, int year) {
   super();
   this.nameString = nameString;
   this.sex = sex;
   this.year = year;
  }

  public String getNameString() {
   return nameString;
  }

  public void setNameString(String nameString) {
   this.nameString = nameString;
  }

  public boolean isSex() {
   return sex;
  }

  public void setSex(boolean sex) {
   this.sex = sex;
  }

  public int getYear() {
   return year;
  }

  public void setYear(int year) {
   this.year = year;
  }

 }

你可能感兴趣的:(java,code,practice)