zoukankan      html  css  js  c++  java
  • Java中List的通俗介绍和简单实例


      List 经常用在对一组对象的存储和操作上,比如一组学生信息,一组帐号信息等等。 
      
      List是个集合接口,只要是集合类接口都会有个“迭代子”( Iterator ),利用这个迭代子,就可以对list内存的一组对象进行操作。 
      
      所有要想操作这个list内存的东西,就首先要得到此迭代子的实例:Iterator it=l.iterator(); 
      
      可以理解为动态数组,传统数组必须定义好数组的个数才可以使用,而容器对象无须定义好数组下标总数。 
      
      用add()方法即可添加新的成员对象,他可以添加的仅仅只能为对象,不能添加基本数据类型,容器还对应get(),remove()方法来获取和删除数据成员
      
      实例1.
      import java.util.*;
      public class ArrayListTest{
      public static void main(String dd[]){
       //new了一个存储list
       List l=new ArrayList();
       //因为Collection framework只能存储对象所以new封装类
       l.add(new Integer(1));
       l.add(new Integer(2));
       l.add(new Integer(3));
       l.add(new Integer(4));
       
       Iterator it=l.iterator();
       //hasNext是取值取的是当前值.他的运算过程是判断下个是否有值如果有继续.
       while(it.hasNext()){
       //设it.next封装类,调用Integer的intValue方法返回值为int赋给i;
       int i=((Integer)it.next()).intValue();
       System.out.println("Element in list is : "+i);
       }
      }
      }
      
      
      实例2.
      import java.util.*;
      public class ArrayListTest1{
      public static void main(String dd[]){
       //new了一个存储list
       List l=new ArrayList();
       //因为Collection framework只能存储对象这个例子就是说明String是对象
       l.add("lalala");
       l.add("afdsfa");
      
       
       Iterator it=l.iterator();
       //hasNext是取值取的是当前值.他的运算过程是判断下个是否有值如果有继续.
       while(it.hasNext()){
       //设it.next封装类,调用强制转换String类型赋值给i;
       String i=(String)it.next();
       System.out.println("Element in list is : "+i);
       }
      }
      } 

  • 相关阅读:
    设计模式的六大原则 ---- 理论知识
    动手编写TCP服务器系列之一:日志文件
    Shell语言系列之一:文件处理
    给Amazon ec2 增加卷(Volume)并挂载到系统
    Java打包问题之一:打包出现java.io.IOException: invalid header field
    struct中长度为0的数组用途与原理
    child和childNodes的区别
    学习es6 setter/getter研究
    tabIndex-bootstrap中Get到的
    tml兼容性
  • 原文地址:https://www.cnblogs.com/renchang/p/4439109.html
Copyright © 2011-2022 走看看