List集合之ArrayList

1.List集合

1.1 List集合中元素有次序、可以重复,可以通过索引来访问集合中的元素。默认按照元素的添加顺序来设置元素的序号,序号从0开始。

1.2 List做为Collection的子接口,继承了Collection接口的所有方法。同时由于List集合的有序性,可以通过序号来访问、删除、设置、获取指定元素序号、获取指定序号元素子集等。具体代码,如下:

List<String> strLists = new ArrayList<String>();
	    String[] str = {"g","h","k"};
	    List<String> arrayToList = Arrays.asList(str);
	    //指定位置插入元素
	    strLists.add(0, "c"); //新建集合若是不从第0个开始插入会报越界错误
	    strLists.add(1, "a");
	    strLists.add(2, "b");
	    System.out.println(strLists);
	    
	    //指定位置插入集合
	    strLists.addAll(1, arrayToList);
	    System.out.println(strLists); //输出:[c, g, h, k, a, b]
	    System.out.println(strLists.get(2)); //输出:h
	    
	    //返回指定元素的序号
	    System.out.println("h:"+strLists.indexOf("h")); //输出 h:2
    	    
	   //元素最后一次出现的位置
	   strLists.add("g"); 
       System.out.println("g最后一次出现的序号:"+strLists.lastIndexOf("g")); //输出:g最后一次出现的序号:6
       System.out.println("g第一次出现的序号:"+strLists.indexOf("g"));   //输出:g第一次出现的序号:1
       
      //移除指定位置的元素
       strLists.remove(0);
       System.out.println(strLists);   //输出:[g, h, k, a, b, g]
       //移除指定元素
       strLists.remove("k");
       System.out.println(strLists);  //输出:[g, h, a, b, g]
       
       //设置某个位置的元素
       strLists.set(0, "j");
       System.out.println(strLists);  //输出:[j, h, a, b, g]
       
       //获取集合的子集
      System.out.println(strLists.subList(0, strLists.size()-2));  //输出:[j, h, a]

1.3 List集合除了有Collection具有的Interator外,还有ListInterator迭代器,多了方法previous()和hasPrevious(),可以实现向前迭代。

//listIterator
      List<String> listIn = new ArrayList<String>();
      listIn.add("a");
      listIn.add("b");
      listIn.add("c");
      ListIterator<String> listRator = listIn.listIterator();
      
      //顺序输出
      while(listRator.hasNext())
      {
    	  System.out.print(" "+listRator.next());
      }
      System.out.println();
      System.out.println("========逆序了=======");
      //逆序输出
      while(listRator.hasPrevious())
      {
    	  System.out.print(" "+listRator.previous());
      }

1.4 ArrayList常用方法


修饰符与类型方法与描述
booleanadd(E e)
Appends the specified element to the end of this list.
voidadd(int index,E element)
Inserts the specified element at the specified position in this list.
booleanaddAll(Collection<? extendsE> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
booleanaddAll(int index,Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
voidclear()
Removes all of the elements from this list.
Objectclone()
Returns a shallow copy of this ArrayList instance.
booleancontains(Object o)
Returns true if this list contains the specified element.
voidensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
Eget(int index)
Returns the element at the specified position in this list.
intindexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
booleanisEmpty()
Returns true if this list contains no elements.
Iterator<E>iterator()
Returns an iterator over the elements in this list in proper sequence.
intlastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
ListIterator<E>listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
ListIterator<E>listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
Eremove(int index)
Removes the element at the specified position in this list.
booleanremove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
booleanremoveAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.
protected voidremoveRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
booleanretainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection.
Eset(int index,E element)
Replaces the element at the specified position in this list with the specified element.
intsize()
Returns the number of elements in this list.
List<E>subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
Object[]toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
<T> T[]toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
voidtrimToSize()
Trims the capacity of this ArrayList instance to be the list's current size.
1.5 ArrayList是线程不安全的,多线程时需要同步线程,Vector是线程安全的,但是性能要比ArrayList低。

1.6 Arrays.toList()方法会将数组转换成固定大小的list集合,若是在转换的list中删除、增加元素时会报错。

1.7 对应list的遍历效率,ArrayList使用get方法遍历效率较高,适合随机方法,LinkedList使用迭代遍历效率较高。



  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值