Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42377
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 726
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-24 08:29
文章分类

全部博文(71)

文章存档

2015年(71)

我的朋友

分类: Java

2015-02-15 21:42:57


  1. package com.imooc.collection;

  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Iterator;
  5. import java.util.List;

  6. /**
  7.  * 备选课程类
  8.  *
  9.  * @author Administrator
  10.  *
  11.  */
  12. public class ListTest {
  13.     /*
  14.      * 用于存放备选课程的List
  15.      */
  16.     public List coursesToSelect;

  17.     public ListTest() {
  18.         this.coursesToSelect = new ArrayList();
  19.     }

  20.     /*
  21.      * 用于往coursesToSelect中添加备选课程
  22.      */
  23.     public void testAdd() {
  24.         // 创建一个对象,并通过调用add方法,添加到备选课程List中
  25.         Course cr1 = new Course("1", "数据结构");
  26.         coursesToSelect.add(cr1);
  27.         // 注意:对象存入集合都变成Object类型,取出时需要类型转换,强转
  28.         Course temp = (Course) coursesToSelect.get(0);
  29.         System.out.println("添加了课程-->" + temp.id + ":" + temp.name);

  30.         Course cr2 = new Course("2", "C语言");
  31.         coursesToSelect.add(0, cr2);
  32.         Course temp2 = (Course) coursesToSelect.get(0);
  33.         System.out.println("添加了课程-->" + temp2.id + ":" + temp2.name);

  34.         coursesToSelect.add(cr1);
  35.         Course temp0 = (Course) coursesToSelect.get(2);
  36.         System.out.println("添加了课程-->" + temp0.id + ":" + temp0.name);

  37.         Course[] course = { new Course("3", "离散数学"), new Course("4", "汇编语言") };
  38.         coursesToSelect.addAll(Arrays.asList(course));
  39.         Course temp3 = (Course) coursesToSelect.get(3);
  40.         Course temp4 = (Course) coursesToSelect.get(4);
  41.         System.out.println("添加了两门课程-->" + temp3.id + ":" + temp3.name + ";"
  42.                 + temp4.id + ":" + temp4.name);

  43.         Course[] course2 = { new Course("5", "高等数学"), new Course("6", "大学英语") };
  44.         coursesToSelect.addAll(2, Arrays.asList(course2));
  45.         Course temp5 = (Course) coursesToSelect.get(2);
  46.         Course temp6 = (Course) coursesToSelect.get(3);
  47.         System.out.println("添加了两门课程-->" + temp5.id + ":" + temp5.name + ";"
  48.                 + temp6.id + ":" + temp6.name);
  49.     }

  50.     /**
  51.      * 取得List中的元素的方法
  52.      *
  53.      * @param args
  54.      */

  55.     public void testGet() {
  56.         int size = coursesToSelect.size();
  57.         System.out.println("有如下课程可选:");
  58.         for (int i = 0; i < size; i++) {
  59.             Course cr = (Course) coursesToSelect.get(i);
  60.             System.out.println("课程-->" + cr.id + cr.name);
  61.         }
  62.     }

  63.     /*
  64.      * 通过迭代器来遍历List
  65.      */

  66.     public void testIterator() {
  67.         // 通过集合的iterator方法,取得迭代器的实例
  68.         Iterator it = coursesToSelect.iterator();
  69.         System.out.println("有如下课程可以选择(通过迭代器访问):");
  70.         while (it.hasNext()) {
  71.             Course cr = (Course) it.next();
  72.             System.out.println("课程:" + cr.id + cr.name);
  73.         }
  74.     }

  75.     /*
  76.      * 通过for each方法访问集合元素
  77.      */

  78.     public void testForEach() {
  79.         System.out.println("有如下课程可以选择(通过foreach访问):");
  80.         for (Object obj : coursesToSelect) {
  81.             Course cr = (Course) obj;
  82.             System.out.println("课程:" + cr.id + cr.name);
  83.         }
  84.     }

  85.     /**
  86.      * 修改List中的元素
  87.      *
  88.      * @param args
  89.      */

  90.     public void testModify() {
  91.         coursesToSelect.set(4, new Course("7", "毛概"));
  92.     }

  93.     public static void main(String[] args) {
  94.         ListTest lt = new ListTest();
  95.         lt.testAdd();
  96.         lt.testGet();
  97.         lt.testIterator();
  98.         lt.testForEach();
  99.         lt.testModify();
  100.         lt.testForEach();
  101.     }
  102. }

阅读(118) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~