首页 > 编程语言 > 详细

java打乱ArrayList生成一个随机ArrayList列表

时间:2014-09-20 11:18:08      阅读:318      评论:0      收藏:0      [点我收藏+]

自己写了一个,有时候会有需要。

public static <V> boolean isEmpty(ArrayList<V> sourceList) {
        return (sourceList == null || sourceList.size() == 0);
    }

/**
     * 打乱ArrayList
     * 
     * */
    public static <V> ArrayList<V> randomList(ArrayList<V> sourceList){
    	if (isEmpty(sourceList)) {
            return sourceList;
        }
    	
    	ArrayList<V> randomList = new ArrayList<V>( sourceList.size( ) );
    	do{
    		int randomIndex = Math.abs( new Random( ).nextInt( sourceList.size() ) );
        	randomList.add( sourceList.remove( randomIndex ) );
    	}while( sourceList.size( ) > 0 );
    	
    	return randomList;
    }


java打乱ArrayList生成一个随机ArrayList列表

原文:http://blog.csdn.net/ekeuy/article/details/39430101

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!