ShuffleElements(随机打乱数组中的元素)
给定一个数组,随机打乱数组中的元素,题意很简单直接上代码:
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(随机打乱数组中的元素)的更多相关文章
- 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果
package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...
- 087-把PHP数组中的元素按随机顺序重新排列shuffle
<?php $arr=array(3,23,'A','f','123','hello'); //定义一个数组 echo '排序之前的数组信息:<br>'; print_r($arr) ...
- LeetCode - 统计数组中的元素
1. 统计数组中元素总结 1.1 统计元素出现的次数 为了统计元素出现的次数,我们肯定需要一个map来记录每个数组以及对应数字出现的频次.这里map的选择比较有讲究: 如果数据的范围有限制,如:只有小 ...
- php 去除数组中重复元素
去除数组中重复元素, 找了下可以一下两个函数 php array_flip()与array_uniqure() $arr = array(…………) ;// 假设有数组包含一万个元素,里面有重复的元素 ...
- 将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件
将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件import java.io.File;import java.io.FileOutputStre ...
- 功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素。
功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素 names={{"tom","jack","mike&qu ...
- 【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 ...
- 数组中的元素 增加push用法 unshift() 方法 和减少pop() 方法 shift() 和其他位置增删 splice() 方法 join() 方法 reverse() 方法 sort() 方法
push用法 push 英 [pʊʃ] 美 [pʊʃ] vt. 推,推动; vt. 按; 推动,增加; 对…施加压力,逼迫; 说服; n. 推,决心; 大规模攻势; 矢志的追求 定义和用法 push( ...
- js向一个数组中插入元素的几个方法-性能比较
向一个数组中插入元素是平时很常见的一件事情.你可以使用push在数组尾部插入元素,可以用unshift在数组头部插入元素,也可以用splice在数组中间插入元素. 但是这些已知的方法,并不意味着没有更 ...
随机推荐
- spring应用中多次读取http post方法中的流(附源码)
一.问题简述 先说下为啥有这个需求,在基于spring的web应用中,一般会在controller层获取http方法body中的数据. 方式1: 比如http请求的content-type为appli ...
- mysql innodb存储引擎优化
innodb_data_home_dir 这是InnoDB表的目录共用设置.如果没有在 my.cnf 进行设置,InnoDB 将使用mysql的datadir目录为缺省目录.如果设定一个空字串,可以i ...
- 【CF839E】Mother of Dragons 折半状压
[CF839E]Mother of Dragons 题意:给你一张n个点,m条边的无向图.你有k点能量,你可以把能量分配到任意一些点上,每个点分到的能量可以是一个非负实数.定义总能量为:对于所有边&l ...
- node path的一些理解笔录
例子假如我们有这样的文件结构: app/ -lib/ -common.js -model -task.js -test.js 执行代码: var path = require("path&q ...
- 在 arc里面打印 引用计数的方法
查阅资料: You can use CFGetRetainCount with Objective-C objects, even under ARC: NSLog(@"Retain c ...
- wpgcms---设置应用模板
wpgcms设置应用模板,找了好半天才找到. 第一步:找到对应的应用(例如:案例) 选择“界面”,前台页面 设置列表模板: 设置详情模板:
- R生存分析AFT
γ = 1/scale =1/0.902 α = exp(−(Intercept)γ)=exp(-(7.111)*γ) > library(survival) > myfit=survre ...
- POJ-1958 Strange Towers of Hanoi(线性动规)
Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2677 Accepted: 17 ...
- POJ-放苹果(DP)
放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29074 Accepted: 18376 Description 把M个 ...
- HDFS Snapshots
Overview HDFS Snapshots are read-only point-in-time copies of the file system. Snapshots can be take ...