java容器新增一組元素

u010660276發表於2014-01-01

Arrays.asList方法接收一個陣列或者一個用逗號隔開的元素列表,轉化為一個list物件。

Collections.addAll()方法接受一個Collection物件和一個陣列或者用逗號隔開的元素列表,將元素新增到Collection物件中。

Collection物件中的addAll方法只接受List物件,將其新增到序列中。

import java.util.*;
public class AddingGroups {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Collection<Integer> collection=new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
		Integer [] moreInts={6,7,8,9};
		collection.addAll(Arrays.asList(moreInts));
		Collections.addAll(collection,11,12,13);
		Collections.addAll(collection,moreInts);
		List<Integer> list=Arrays.asList(16,17,18,19);
		list.set(1,99);
		Map<String,Integer> map=new HashMap<String,Integer>();//Map是抽象類,這裡要用Integer而不能是int
		map.put("apple",1);
		System.out.println(collection);
		System.out.println(list);
		System.out.println(map);

	}

}


相關文章