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

浙公网安备 33010602011771号