ArrayList面试题,array和arraylist的区别面试题

  ArrayList面试题,array和arraylist的区别面试题

  00-1010代码复制源代码分析附:ArrayList子列表简介及使用总结。

  00-1010不,想想会打印什么?

  ListString list 1=new ArrayList(arrays . aslist( username , passwd ));ListString list 2=list 1 . sublist(0,2);list 2 . add( email );system . out . println(list 1);system . out . println(list 2);执行结果:

  你感到困惑吗?我想知道为什么列表2中添加的内容也会添加到列表1中,对吗?

  

目录

子列表界面

 

  ListE子列表(int fromIndex,int to index);我们用数组列表,所以选择数组列表就好了。

  public ListE子列表(int fromIndex,int toIndex){ subListRangeCheck(from index,to index,size);返回新的子列表(this,0,fromIndex,toIndex);}fromIndex是从列表元素开始的索引,toIndex是从列表元素结束的索引,subListRangeCheck方法是检查是否在允许的范围内。

  static void sublistrangecheck(int from index,int toindex,int size){//起始索引小于0 if(from index 0)throw new indexoutofboundsexception( from index= from index);//结束索引大于容量,如果(toindex size)抛出新的indexoutofBoundsException( toindex= toindex );//开始索引大于结束索引if(from index to index)throw new illegalargumentexception( from index( from index )to index( toindex ));主事件在新子列表中(this,0,from index,toindex);在这里,你看下面的子列表,就知道这个关键字把当前对象即list1的引用传入子列表,把传入的list1改成parent赋给子列表的内部成员,然后赋给list2,也就是说list1和list2引用同一个对象,指向同一个列表。

  Sublist (abstract listen parent,int offset,int from index,int to index){//就是这个问题。Parent=parentthis . parent offset=from index;this.offset=从Index的偏移量;this . size=to index-from index;this . modcount=ArrayList . this . modcount;}我们来看看list2.add的源代码,直接在list1和list2的公共列表引用对象中添加元素。所以才加了list2,list1也是。

  public void add(int index,E E){ rangeCheckForAdd(index);checkforcomedification();//将元素直接添加到list1和list2的公共列表引用对象parent.add(parentOffset index,e);this . mod count=parent . mod count;this.size}

  

代码复现

subList(int fromIndex, int toIndex);

 

  它返回原列表的[fromIndex,toIndex]之间的部分,实际上是列表的子列表(注:fromIndex是[描述包含自身,toIndex是]而描述不包含自身)。

  这个子列表的本质其实是原列表的一部分;换句话说,修改这个子列表会导致原始列表发生变化。

  举例说明

  列表包含6个元素:1、2、3、4、5和6,list.subList(1,3)返回2,3(列表从0开始)。

  还有一个常用的list.subList(1,list.size)。

  该列表包含六个元素:1、2、3、4、5和6。list.subList(1,list.size)返回2,3,4,5和6(列表从0开始)。

  00-1010关于Java中数组列表和子列表的坑面试题这篇文章到此为止。更多关于ArrayList和SubList的面试问题,请搜索热门IT之前的文章或者继续浏览下面的相关文章。我希望你以后能更多地支持流行音乐!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: