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

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. 为android编译libsocket的脚本

    #!/bin/bash U32=0 #编译64位arm时 U32=0   编译32位arm时 U32=1 其他参数不需要变动 TARGET=android-24 HOST=darwin-x86_64 ...

  2. LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)

    题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description   Problem :当前的数组 ...

  3. sencha touch 在线实战培训 第一期 第四节

    2014.1.4晚上8点开的课 第一节收费课程,还是有几位同学付费了,这些课程也录像了的,以后也会持续销售. 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内容:          ...

  4. rsyslog local0-local7的用法

    很多时候我们需要将一个服务的日志文件导向一个指定的文件,这个时候可以设置log-facility 如在dhcpd.conf中配置 1 : update log-facility in the dhcp ...

  5. Metaspolit使用简介

    信息收集阶段 Whois信息收集 msf auxiliary > whois baidu.com 域名信息查询阶段 msf auxiliary > nslookup www.baidu.c ...

  6. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十:PS/2模块④ — 普通鼠标

    实验十:PS/2模块④ - 普通鼠标 学习PS/2键盘以后,接下来就要学习 PS/2 鼠标.PS/2鼠标相较PS/2键盘,驱动难度稍微高了一点点,因为FPGA(从机)不仅仅是从PS/2鼠标哪里读取数据 ...

  7. 【转】C内存管理

    在任何程序设计环境及语言中,内存管理都十分重要.在目前的计算机系统或嵌入式系统中,内存资源仍然是有限的.因此在程序设计中,有效地管理内存资源是程序员首先考虑的问题. 第1节主要介绍内存管理基本概念,重 ...

  8. Java秒杀简单设计四:service层设计

    接上一篇 https://www.cnblogs.com/taiguyiba/p/9829191.html  封装了系统传递的数据类和异常类 本文继续设计service层设计: 1.SeckillSe ...

  9. tomcat如何配置启动时自动部署webapps下的war包

    1.找到 tomcat安装目录/conf/server.xml 2.修改host元素的配置如下: <Host name="localhost" appBase="w ...

  10. srilm使用杂记

    训练n-gram语言模型 ngram-count -text train.txt -order -lm model -kndiscount -interpolate -gt3min -gt4min 计 ...