Java ArrayList

// ArrayList
ArrayList<String> myArr =  new ArrayList<String>();
myArr.add("one");
myArr.add("two");
System.out.println(myArr.get(0));


//Vector 
Vector<String> myV = new Vector<String> ();
myV.add("one");
myV.add("two");
System.out.println(myArr.get(0));

 

ArrayList 和 Vector 区别:

Vector是线程安全级的,即某一时刻只有一个线程能写Vector. 避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。一般用ArrayList比较多

 

遍历ArrayList:

  //方法1
  Iterator it1 = list.iterator();
  while(it1.hasNext()){
  System.out.println(it1.next());
  }
  //方法2  怪异!
  for(Iterator it2 = list.iterator();it2.hasNext();){
  System.out.println(it2.next());
  }
  //方法3
  for(String tmp:list){
  System.out.println(tmp);
  }
  //方法4
  for(int i = 0;i < list.size(); i ++){
  System.out.println(list.get(i));
  }

 

 

常用函数:

add(element)  // add element to the end

add(index, element)  //add element to the specified position

addAll(index, Collection)  //Inserts all of the elements in the specified collection into this list, starting at the specified position

 

clear() //remove all elements from the list

clone() // return a shallow copy of this ArrayList instance

contains(o) // Returns true if this list contains the specified element. 

 

get(index) // return object at index 

indexOf(object)  // return index of the first occurence of the object 

isEmpty() // returns true if is empty

remove(index) // remove element on index 

set(index, element) // 把index处的元素设置成element

toArray() // 把ArrayList转变成array

 

iterator() return iterator  

转载于:https://www.cnblogs.com/XingyingLiu/p/5150844.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值