更新時間:2021-05-14 10:58:20 來源:動力節點 瀏覽1357次
為了更好地理解這個題意,我們先來看下具體內容:生成一個1-100的隨機數組,但數組中的數字不能重復,即位置是隨機的,但數組元素不能重復。
在這里呢,沒有給我們規定數組的長度,我們可以讓它是1-100之間的任意長度。
接下來讓我們看一下幾種實現方法并對這幾種方法作個對比。
通常我們會使用ArrayList或數組來實現,先來看下ArrayList實現過程,如下面代碼所示:
import java.util.ArrayList;
import java.util.Random;
/**
* 使用ArrayList實現
* @Description:
* @File: Demo.java
* @Date 2012-10-18 下午06:16:55
* @Version V1.0
*/
public class Demo {
public static void main(String[] args) {
Object[] values = new Object[20];
Random random = new Random();
ArrayList list = new ArrayList();
for(int i = 0; i < values.length;i++){
int number = random.nextInt(100) + 1;
if(!list.contains(number)){
list.add(number);
}
}
values = list.toArray();
// 遍歷數組并打印數據
for(int i = 0;i < values.length;i++){
System.out.print(values[i] + "\t");
if(( i + 1 ) % 10 == 0){
System.out.println("\n");
}
}
}
}
使用數組實現的過程如下所示代碼:
import java.util.Random;
/**
* 使用數組實現
* @Description:
* @File: Demo4.java
* @Package None
* @Author Hanyonglu
* @Date 2012-10-18 下午06:27:38
* @Version V1.0
*/
public class Demo4 {
public static void main(String[] args) {
int[] values = new int[20];
Random random = new Random();
for(int i = 0;i < values.length;i++){
int number = random.nextInt(100) + 1;
for(int j = 0;j <= i;j++){
if(number != values[j]){
values[i]=number;
}
}
}
// 遍歷數組并打印數據
for(int i = 0;i < values.length;i++){
System.out.print(values[i] + "\t");
if(( i + 1 ) % 10 == 0){
System.out.println("\n");
}
}
}
}
上面這兩個實現過程效率比較低的。因為在每次添加時都要去遍歷一下當前列表中是否存在這個數字,時間復雜度是O(N^2)。我們可以這樣思考一下:既然涉及到無重復,我們可以想一下HashSet和HashMap的功能。
HashSet實現Set接口,Set在數學上的定義就是無重復,無次序的集合。而HashMap實現Map,也是不允許重復的Key。這樣我們可以使用HashMap或HashSet來實現。
在使用HashMap實現時,只需要將它的key轉化成數組就Ok了,如下代碼:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.Map.Entry;
/**
* 使用HashMap實現
* @Description:
* @File: Demo.java
* @Package None
* @Author Hanyonglu
* @Date 2012-10-18 下午06:12:50
* @Version V1.0
*/
public class Demo {
public static void main(String[] args) {
int n = 0;
Object[] values = new Object[20];
Random random = new Random();
HashMap hashMap = new HashMap();
// 生成隨機數字并存入HashMap
for(int i = 0;i < values.length;i++){
int number = random.nextInt(100) + 1;
hashMap.put(number, i);
}
// 從HashMap導入數組
values = hashMap.keySet().toArray();
// 遍歷數組并打印數據
for(int i = 0;i < values.length;i++){
System.out.print(values[i] + "\t");
if(( i + 1 ) % 10 == 0){
System.out.println("\n");
}
}
// Iterator iter = hashMap.entrySet().iterator();
// // 遍歷HashMap
// while (iter.hasNext()) {
// Entry entry = (Entry)iter.next();
// int key = entry.getKey();
// n++;
//
// System.out.print(key + "\t");
//
// if(n % 10 == 0){
// System.out.println("\n");
// }
// }
}
}
由于HashSet和HashMap的關系太近了,HashSet在底層就是用HashMap來實現的,只不過沒有Value的集合,只有一個Key的集合,所以也可使用HashSet來實現,如下代碼:
import java.util.HashSet;
import java.util.Random;
/**
* 使用HashSet實現
* @Description:
* @File: Test.java
* @Package None
* @Author Hanyonglu
* @Date 2012-10-18 下午06:11:41
* @Version V1.0
*/
public class Test {
public static void main(String[] args) {
Random random = new Random();
Object[] values = new Object[20];
HashSet hashSet = new HashSet();
// 生成隨機數字并存入HashSet
for(int i = 0;i < values.length;i++){
int number = random.nextInt(100) + 1;
hashSet.add(number);
}
values = hashSet.toArray();
// 遍歷數組并打印數據
for(int i = 0;i < values.length;i++){
System.out.print(values[i] + "\t");
if(( i + 1 ) % 10 == 0){
System.out.println("\n");
}
}
}
}
這樣實現效率稍微好些。如果給我們限定了數組的長度,只需要變換下for循環,設置成whlie循環就可以了。如下所示:
import java.util.HashSet;
import java.util.Random;
/**
* 使用HashSet實現
* @Description:
* @File: Test.java
* @Package None
* @Author Hanyonglu
* @Date 2012-10-18 下午05:11:41
* @Version V1.0
*/
public class Test {
public static void main(String[] args) {
Random random = new Random();
Object[] values = new Object[20];
HashSet hashSet = new HashSet();
// 生成隨機數字并存入HashSet
while(hashSet.size() < values.length){
hashSet.add(random.nextInt(100) + 1);
}
values = hashSet.toArray();
// 遍歷數組并打印數據
for(int i = 0;i < values.length;i++){
System.out.print(values[i] + "\t");
if(( i + 1 ) % 10 == 0){
System.out.println("\n");
}
}
}
}
以上幾種相比較而言,使用HashMap的效率是比較高的,其實是HashSet,再次是數組,最后是ArrayList。如果我們生成10000個數據將會發現,使用HashMap花費時間是:0.05s,HashSet是0.07s,數組是:0.20s,而ArrayList是0.25s。有興趣的可以設置下時間查看一下。
當然了,除了使用HashMap實現外,還有其它高效的方法。比如,我們可以把1-100這些數字存儲在一個數組中,然后在for循環中隨機產生兩個下標,如果這兩個下標不相等的話,可以交換數組中的元素,實現過程如下所示:
import java.util.Random;
/**
* 隨機調換位置實現
* @Description:
* @File: Demo4.java
* @Package None
* @Author Hanyonglu
* @Date 2012-10-18 下午06:54:06
* @Version V1.0
*/
public class Demo4 {
public static void main(String[] args) {
int values[] = new int[100];
int temp1,temp2,temp3;
Random r = new Random();
for(int i = 0;i < values.length;i++){
values[i] = i + 1;
}
//隨機交換values.length次
for(int i = 0;i < values.length;i++){
temp1 = Math.abs(r.nextInt()) % (values.length-1); //隨機產生一個位置
temp2 = Math.abs(r.nextInt()) % (values.length-1); //隨機產生另一個位置
if(temp1 != temp2){
temp3 = values[temp1];
values[temp1] = values[temp2];
values[temp2] = temp3;
}
}
// 遍歷數組并打印數據
for(int i = 0;i < 20;i++){
System.out.print(values[i] + "\t");
if(( i + 1 ) % 10 == 0){
System.out.println("\n");
}
}
}
}
以上就是動力節點小編介紹的"Java不重復隨機數的生成方法總結",希望對大家有幫助,如有疑問,請在線咨詢,有專業老師隨時為您服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習