JR 精品文章 - 有趣的List
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » JDK核心API 评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
有趣的List
sheldonsun 原创   更新:2006-11-29 12:18:56  版本: 1.0   

List<String> list = new ArrayList<String>();
list.add(1,"111");
list.add(2,"222");
list.add(2,"333");
list.add(3,"444");
String str1 = list.get(2);
String str2 = list.get(3);
int size = list.size();

结果:
str1 = 333;
str2 = 222;
size = 4;

ArrayList代码如下:
public void add(int index, E element) {
    if (index > size || index < 0)
        throw new IndexOutOfBoundsException(
        "Index: "+index+", Size: "+size);

    ensureCapacity(size+1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
             size - index);
    elementData[index] = element;
    size++;
    }
ArrayList是基于数组存储对象的, 所以第一步,    ensureCapacity(size+1);  加大数组的长度, 然后把index后的所以值一次后移动, 然后插入新的数据, LinkList是基于链表的存储, 实现的功能基本一置..... map, set类似.




版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     13       9
作者其它文章: 作者全部文章
评论人:wf_chn 发表时间: Wed Nov 29 12:56:07 CST 2006
照此理解
str2应该等于444吧
评论人:hyhongyong 发表时间: Wed Nov 29 13:44:59 CST 2006
这有趣吗?[:x]
评论人:hechangmin 发表时间: Wed Nov 29 13:57:59 CST 2006
算不算是jdk的bug呢?

评论人:joy_here 发表时间: Wed Nov 29 13:58:39 CST 2006
when executing: list.add(1, "111");
you don't get exception??[:o]
评论人:hechangmin 发表时间: Wed Nov 29 14:00:27 CST 2006
 你说的应该算bug?

我总结一个容易忽略的地方(不是BUG),比如HASHMAP用HASHCODE来存储数据顺序的。
下面程序输入结果是:
4
one=gege
two=gegege
four=gegegegege
three=gegegege
所以不会是:
four=gegegegege
three=gegegege
的顺序。
public class Main
{
   public Main ()
    {
       
    }
   public static void main (String[] args)
    {
       HashMap map = new HashMap ();
       map.put (new String("one"),new String ("gege"));
       map.put (new String("two"),new String ("gegege"));
       map.put (new String("three"),new String ("gegegege"));
       map.put (new String("four"),new String ("gegegegege"));
       //遍历 hashmap
       System.out.println (map.size ());
       Iterator itr = map.keySet ().iterator ();
       while(itr.hasNext ())
       {
            Object temp1 = itr.next ();
            Object temp2 = map.get (temp1);
            System.out.println (temp1.toString ()+"="+temp2.toString ());
       }
    }
    
}
评论人:shylanse 发表时间: Wed Nov 29 15:45:17 CST 2006
[:?]
评论人:cherami 发表时间: Wed Nov 29 20:05:23 CST 2006
没有看出这篇文章的意义,程序的结果很正常。
评论人:imyf_liang 发表时间: Thu Nov 30 08:48:21 CST 2006
首先你这里的错字别字太多了!!!!
第二如果你程序运行的时候肯定会抛出数组越界错误!!!!

因为List必须从下标0开始添加元素


第三,像你说的,下标相同的以最后的一个值为主,前面的都忽略,绝对不会影响到它以后的下标的的值。

你可以详细看看jdk的源代码,到底怎么实现的!!!!!


哗众取宠,而且还在这里误导别人!!!!!!!
评论人:littleredboy 发表时间: Thu Nov 30 09:57:38 CST 2006
如果你想有排序的话,请用LinkedHashMap
ArrayList没排序
评论人:jerimy 发表时间: Thu Nov 30 10:15:37 CST 2006
鸡肋文章。
评论人:afeng217 发表时间: Thu Nov 30 10:26:22 CST 2006
对楼主的代码作了一些测试,问题如下:

1. List必须从下标0开始添加元素,按照楼主的用法,抛出异常如下(怀疑楼主没对代码验证):
 java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

2.以下根据不同的情况,我作的一些测试(提供现象,给大家参考)
测试1:
                  List<String> list = new ArrayList<String>();
                  list.add(0,"111");
                  list.add(1,"222");
                  list.add(2,"333");
                  list.add(3,"444");
                  String str1 = list.get(2);
                  String str2 = list.get(3);
                  System.out.println(str1 + " , " + str2);
输出结果: 333 , 444

测试2: 
                  list.add(0,"111");
                  list.add(1,"222");
        list.add(2,"333");
        list.add(2,"444");
        String str1 = list.get(2);
        String str2 = list.get(3);
                  System.out.println(str1 + " , " + str2);
输出结果: 444 , 333
测试3:
                  list.add(0,"111");
        list.add(1,"222");
        list.add(1,"333");
        list.add(2,"444");
        String str1 = list.get(2);
        String str2 = list.get(3); 
                  System.out.println(str1 + " , " + str2);
输出结果: 444 , 222

测试4: 
                  list.add(0,"111");
        list.add(1,"222");
        list.add(1,"333");
        list.add(3,"444");
        String str1 = list.get(2);
        String str2 = list.get(3);
                  System.out.println(str1 + " , " + str2);
输出结果: 222 , 444

结论1: 根据测试2和测试3,测试4,可以看出,角标相同的话,会覆盖前面的值,而前面的值会被往后移到下一个[还没有被赋值的,角标位置](从测试结果中看出的,如果有误,希望大家不要拿鸡蛋砸我,并回复告知!!)

另外,每次测试我都对整个,list作输出: 
                  for (Iterator<String> i = list.iterator(); i.hasNext();) {
            System.out.println(i.next());
        }
结论2: 结果更清晰了,更说明结论1(就不提供输出结果了,大家自己测试了,会更清楚)

小小程序员,小小测试结果,希望对大家有帮助!![:)]
评论人:cumt327 发表时间: Thu Nov 30 12:47:42 CST 2006
楼主不厚道。
JDK5。0里说的非常清楚。
原文:
add
void add(int index,
         E element)
Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). 

插入新元素到index位置。如果index位置有元素,index位置包括以后的都依次往后移动。
评论人:ryan1982 发表时间: Thu Nov 30 14:59:36 CST 2006
应该会抛IndexOutOfBoundsException才对
评论人:littleredboy 发表时间: Fri Dec 01 12:21:40 CST 2006
linkedhashmap
评论人:人王 发表时间: Fri Dec 01 15:46:14 CST 2006
这样的帖子
也是经过管理员筛选的?!
JR负点责任行吗?!
别毁了这样好的平台!!![a][a]
评论人:ryan1982 发表时间: Sat Dec 02 10:27:33 CST 2006
对List的理解的又加深了不少,谢谢各位大师
评论人:mrou2001 发表时间: Wed Dec 06 22:04:09 CST 2006
不错的文章[java]
评论人:westlifesz 发表时间: Sat Dec 09 20:39:59 CST 2006
thank you
评论人:lanxiazhi 发表时间: Sat Mar 17 19:39:19 CST 2007
经过测试(jre6)后发现:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
请问楼主用的是哪个版本的jre?
评论人:fan-R 发表时间: Thu Jun 26 20:27:47 CST 2008
楼主太混乱了,都没经过测试

这个文章共有 20 条评论
 
返回文章列表 返回〔JDK核心API〕
下一篇文章 主题: 为什么程序员不应调用“sun”包?


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号