当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:16:00  来源:本站整理

JAVA容器小结[Java编程]

赞助商链接



  本文“JAVA容器小结[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

JAVA中的容器供应了非常完善的办法来保存对象,你可以利用这些工具来办理大数据量的问题.下面是笔者在开辟顶用到过的一些容器总结如此.

1 ArrayList

利用ArrayList非常简单:成立一个实例,用add()插入对象,然后用get()拜候这些对象,此时需求索引,就象数组一样,但是不需求方括号,ArrayList还有size()办法,从而可以知道ArrayList的大小,也可以避免因为越界而引发错误.别的,ArrayList擅长随机拜候元素,但是在List的中间插入和移处元素时较慢.下面是ArrayList的例子(来自thinking in java):

/**//*
* @(#)AppleAndOrangesWithGenerics.java  1.0 May 17, 2008
* @author:Administrator
* Copyright 2008 CSS WEB Microsystems, Inc. All rights reserved.
* CSS WEB ROOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package holding;
import java.util.ArrayList;
public class AppleAndOrangesWithGenerics ...{
public static void main(String[] args) ...{
ArrayList<Apple> apples = new ArrayList<Apple>();
for(int i = 0; i < 3; i++) ...{
apples.add(new Apple());
}
for(int i = 0; i < apples.size(); i++)
System.out.println(((Apple)apples.get(i)).id());
for(Apple c:apples)
System.out.println(c.id()+"-");
}
}
/**//*
* @(#)ListFeatures.java  1.0 May 17, 2008
* @author:Administrator
* Copyright 2008 CSS WEB Microsystems, Inc. All rights reserved.
* CSS WEB ROOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package holding;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import typeinfo.pets.*;
public class ListFeatures ...{
public static void main(String[] args) ...{
Random rand = new Random(47);
List<Pet> pets = Pets.arrayList(7);
System.out.print("1: " + pets);
System.out.println();
Hamster h = new Hamster();
pets.add(h);
System.out.print("2: " + pets);
System.out.println();
System.out.print("3: " + pets.contains(h));
System.out.println();
pets.remove(h);
Pet p = pets.get(2);
System.out.print("4: " + pets.indexOf(p));
System.out.println();
Pet cymric = new Cymric();
System.out.print("5: " + pets.indexOf(cymric));
System.out.println();
System.out.print("6: " + pets.remove(cymric));
System.out.println();
System.out.print("7: " + pets.remove(p));
System.out.println();
System.out.print("8: " + pets);
System.out.println();
pets.add(3,new Mouse());
System.out.print("9: " + pets);
System.out.println();
List<Pet> sub = pets.subList(1,4);
System.out.print("sublist: " + sub);
System.out.println();
System.out.print("10: " + pets.containsAll(sub));
System.out.println();
Collections.sort(sub);
System.out.print("sorted sublist: " + sub);
System.out.println();
System.out.print("11: " + pets.containsAll(sub));
System.out.println();
Collections.shuffle(sub, rand);
System.out.print("shuffle sublist: " + sub);
System.out.println();
System.out.print("12: " + pets.containsAll(sub));
System.out.println();
List<Pet> copy = new ArrayList<Pet>(pets);
sub = Arrays.asList(pets.get(1),pets.get(4));
System.out.print("sub: " + sub);
System.out.println();
copy.retainAll(sub);
System.out.print("13: " + copy);
System.out.println();
copy = new ArrayList<Pet>(pets);
copy.remove(2);
System.out.print("14: " + copy);
System.out.println();
copy.removeAll(sub);
System.out.print("15: " + copy);
System.out.println();
copy.set(1, new Mouse());//Replace an element
System.out.print("16: " + copy);
System.out.println();
copy.addAll(2, sub);
System.out.print("17: " + copy);
System.out.println();
System.out.print("18: " + pets.isEmpty());
System.out.println();
pets.clear();
System.out.print("19: " + pets);
System.out.println();
System.out.print("20: " + pets.isEmpty());
System.out.println();
pets.addAll(Pets.arrayList(4));
System.out.print("21: " + pets);
System.out.println();
Object[] o = pets.toArray();
System.out.print("22: " + o[3]);
System.out.println();
Pet[] pa = pets.toArray(new Pet[0]);
System.out.print("23: " + pa[3].id());
System.out.println();
}
}


  以上是“JAVA容器小结[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .