`
imaginecup
  • 浏览: 85463 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

持有对象Arrays.asList异常解决办法

阅读更多
添加一组元素
package com.day1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class AddingGroups {

	/**
	 * 添加一组元素
	 */
	public static void main(String[] args) {
		//Arrays此类包含用来操作数组(比如排序和搜索)的各种方法。此类还包含一个允许将数组作为列表来查看的静态工厂。
		Collection<Integer> collection=new ArrayList<Integer>(Arrays.asList(1,2,3,4,5)) ;
		Integer[] moreInts={6,7,8,9,10};
		collection.addAll(Arrays.asList(moreInts));
		Collections.addAll(collection, 11,12,13,14,15);
		Collections.addAll(collection, moreInts);
		List<Integer> list=Arrays.asList(16,17,18,19,20);
		/*
		 *如果直接使用Arrays.asList()的输出来创建List,会产生问题 ,因为在这种情况下,其底层表示的
		 *是数组,因此不能调整其大小。如果你试图用add()或delete()方法在这种列表中添加或删除元素
		 * ,就有可能会引发去改变数组尺寸的尝试,因此你将在运行时获得java.lang.UnsupportedOperationException
		 * 的(不支持的操作)异常。解决办法:List<Integer> list=new ArrayList<Integer>(Arrays.asList(1,2,3));
		 * */

		//list.add(21);
		list.set(1, 99);
		for(Integer i:collection){
			System.out.print(i+",");
		}
		System.out.println();
		for(Integer i:list){
			System.out.print(i+",");
		}
		
		

	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics