How to use ArrayList in Java

一般使用ArrayList,而vector和hashtable里的方法都是同步的,所以如果对同步没有必须的要求,使用ArrayList就可以了。
ArrayList man=new ArrayList();
man.add(Object x);
man.add(Object y);..........
这是往该arraylist数组里放数据
man.get[i]取得数组里i位置的元素,取出时是object型,你可以根据自己的需要来重新改变类型。
还有一个set(i,Object obj)的方法是将该数组里的第i个元素赋成obj

 

可以遍历这个list,方法有两种,其中比较通俗的如下:
for( int i=0; i < files.size(); i++){
   System.out.println(files.get(i));
 }
 另外一种是通过获取该List的Iterator迭代器,这两种方法差不多,随便选一种即可。

The ArrayList class extends AbstractList and implements theListinterface. ArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. But, sometimes, you may not know until run time precisely how large of an array you need. To handle this situation, the collections framework definesArrayList. In essence, anArrayList is a variable-length array of object references. That is, anArrayListcan dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

ArrayList has the constructors shown here:
ArrayList( )
ArrayList(Collection c)
ArrayList(int capacity)

The first constructor builds an empty array list. The second constructor builds an array list that is initialized with the elements of the collectionc. The third constructor builds an array list that has the specified initialcapacity. The capacity is the size of the underlying array that is used to store the elements. The capacity grows automatically as elements are added to an array list.

The following program shows a simple use of ArrayList. An array list is created, and then objects of typeStringare added to it. (Recall that a quoted string is translated into aStringobject.) The list is then displayed. Some of the elements are removed and the list is displayed again.

// Demonstrate ArrayList.
import java.util.*;
class ArrayListDemo {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
System.out.println("Initial size of al: " +
al.size());
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " +
al.size());
// display the array list
System.out.println("Contents of al: " + al);
// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " +
al.size());
System.out.println("Contents of al: " + al);
}
}

 

The output from this program is shown here:

Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]

Notice that a1 starts out empty and grows as elements are added to it. When elements are removed, its size is reduced.

Although the capacity of an ArrayList object increases automatically as objects are stored in it, you can increase the capacity of anArrayListobject manually by calling ensureCapacity( ). You might want to do this if you know in advance that you will be storing many more items in the collection that it can currently hold. By increasing its capacity once, at the start, you can prevent several reallocations later. Because reallocations are costly in terms of time, preventing unnecessary ones improves performance. The signature forensureCapacity( )is shown here:

void ensureCapacity(int cap)

Here, cap is the new capacity. Conversely, if you want to reduce the size of the array that underlies anArrayListobject so that it is precisely as large as the number of items that it is currently holding, call
trimToSize( )
, shown here:

void trimToSize( )

Obtaining an Array from an ArrayList

When working with ArrayList, you will sometimes want to obtain an actual array that contains the contents of the list. As explained earlier, you can do this by callingtoArray( ). Several reasons exist why you might want to convert a collection into an array such as:

• To obtain faster processing times for certain operations.
• To pass an array to a method that is not overloaded to accept a collection.
• To integrate your newer, collection-based code with legacy code that does not understand collections.

Whatever the reason, converting an ArrayList to an array is a trivial matter, as the following program shows:

// get array
Object ia[] = al.toArray();
int sum = 0;
// sum the array
for(int i=0; i<ia.length; i++)
sum += ((Integer) ia[i]).intValue();
System.out.println("Sum is: " + sum);
}
}

The output from the program is shown here:
Contents of al: [1, 2, 3, 4]
Sum is: 10

The program begins by creating a collection of integers. As explained, you cannot store primitive types in a collection, so objects of typeIntegerare created and stored. Next, toArray( ) is called and it obtains an array ofObjects. The contents of this array are cast toInteger, and then the values are summed.


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值