给定一个数组,随机打乱数组中的元素,题意很简单直接上代码:

package Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class ShuffleElements { public static void main(String[] args) {
int [] array = {1,2,3,4,5};
//随机打乱数组中的元素
//int [] result = randomizeArray(1,10);
shuffle();
//打印数组中的元素
//print(result);
} /**
* 打印数组中的元素
* @param result
*/
private static void print(int[] result) {
if (result != null && result.length>0) {
for (int i = 0;i<result.length;i++) {
System.out.print(result[i]+" ");
}
System.out.println();
} } /**
* 随机打乱数组中的数字
* @param array
* @return
*/
public static int[] randomizeArray(int [] array){
Random gen = new Random();
for (int i=0;i<array.length;i++) {
int randomPosition = gen.nextInt(array.length);//生成0-array.length区间的随机数
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
} return array; } /**
* 随机打乱a-b区间的数据
* @param a
* @param b
* @return
*/
public static int[] randomizeArray(int a,int b){
Random rgen = new Random(); int size = b-a+1;//数组的大小
int [] array = new int[size];
for (int i = a;i<size;i++) {
array[i] = a+i;
}
for (int i=0;i<array.length;i++) {
int randomPosition = rgen.nextInt(array.length);//生成0-array.length区间的随机数
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
} print(array);
return array;
} //jdk中的shuffle方法
public static void shuffle(){
int[] array = new int[]{1,2,3,4,5};
Collections.shuffle(Arrays.asList(array));
print(array);
} }

  

ShuffleElements(随机打乱数组中的元素)的更多相关文章

  1. 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果

    package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...

  2. 087-把PHP数组中的元素按随机顺序重新排列shuffle

    <?php $arr=array(3,23,'A','f','123','hello'); //定义一个数组 echo '排序之前的数组信息:<br>'; print_r($arr) ...

  3. LeetCode - 统计数组中的元素

    1. 统计数组中元素总结 1.1 统计元素出现的次数 为了统计元素出现的次数,我们肯定需要一个map来记录每个数组以及对应数字出现的频次.这里map的选择比较有讲究: 如果数据的范围有限制,如:只有小 ...

  4. php 去除数组中重复元素

    去除数组中重复元素, 找了下可以一下两个函数 php array_flip()与array_uniqure() $arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素 ...

  5. 将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件

      将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件import java.io.File;import java.io.FileOutputStre ...

  6. 功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素。

    功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素 names={{"tom","jack","mike&qu ...

  7. 【LeetCode每天一题】Find First and Last Position of Element in Sorted Array(找到排序数组中指定元素的开始和结束下标)

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  8. 数组中的元素 增加push用法 unshift() 方法 和减少pop() 方法 shift() 和其他位置增删 splice() 方法 join() 方法 reverse() 方法 sort() 方法

    push用法 push 英 [pʊʃ] 美 [pʊʃ] vt. 推,推动; vt. 按; 推动,增加; 对…施加压力,逼迫; 说服; n. 推,决心; 大规模攻势; 矢志的追求 定义和用法 push( ...

  9. js向一个数组中插入元素的几个方法-性能比较

    向一个数组中插入元素是平时很常见的一件事情.你可以使用push在数组尾部插入元素,可以用unshift在数组头部插入元素,也可以用splice在数组中间插入元素. 但是这些已知的方法,并不意味着没有更 ...

随机推荐

  1. git查看指令

    打开git bash 1,查看自己之前是否生成过ssh密钥 $ ls .ssh 如果存在这个id_rsa.pub这个文件的话表示已经生成了 2,查看用户名和邮箱 $ git config --glob ...

  2. Egret中的三种单例写法

    1 普通的单例写法 as3中也是这么个写法. 缺点:每个单例类里都要写instance和getInstance. class Single{ private static instance:Singl ...

  3. react+babel+webpack初试

    在上一篇,我们简单学习了webpack学习,现在这里我们简单学习一下react+babel+webpack,进行编译react语法jsx以及结合es6写法. 这里我就简单的直接上demo: packa ...

  4. Bagging和Boosting的概念与区别

    随机森林属于集成学习(ensemble learning)中的bagging算法,在集成算法中主要分为bagging算法与boosting算法, Bagging算法(套袋发) bagging的算法过程 ...

  5. Linux磁盘处理

    查看磁盘占用率 df -l 既然确定了哪块磁盘占用率高,那就切换到这块磁盘检查一下这块磁盘的哪个文件夹占用高,再逐层去查找 du -h --max-depth=1

  6. 慕课网,vue高仿饿了吗ASP源码视频笔记

    1.源码笔记 我的源码+笔记(很重要):http://pan.baidu.com/s/1geI4i2Z 感谢麦子学院项目相关视频 2.参考资料 Vue.js官网(https://vuejs.org.c ...

  7. HDU 4848 - Wow! Such Conquering!

    Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descriptio ...

  8. 希尔排序之python

    希尔排序( Shell sort) 插入排序的改进版本,其核心思想是将原数据集合分割成若干个子序列,然后再对子序列分别进行直接插入排序,使子序列基本有序,最后再对全体记录进行一次直接插入排序. 我的面 ...

  9. 表优化 altering table OPTIMIZE TABLE `sta_addr_copy`

    表优化 altering table OPTIMIZE TABLE `sta_addr_copy` [总结] 1.实际测试的结果是,在state sqlaltering table OPTIMIZE ...

  10. Nginx日志切割之Logrotate篇

    不管是什么日志文件,都是会越来越大的,大到一定程度就是个可怕的事情了,所以要及早的做处理,方法之一就是按时间段来存储,不过linux系统提供了Logrotate的日志管理工具,很好用,不用写计划任务脚 ...