Generally, friends with some development experience can realize such a function, but it is just a matter of efficiency. When we usually face such a problem, we always think of it in flat order. First, generate an array, and then add random numbers to the array in a loop. During the process of adding numbers, first check whether they exist in the array. If this number does not exist, it will be added directly to the array; if it exists, it will not be added. We generally think about problems in this way, and functions can be achieved by thinking in this way. As I said just now, it is just a matter of efficiency.
In order to better understand the meaning of this question, let's first look at the specific content: generate a random array of 1-100, but the numbers in the array cannot be repeated, that is, the position is random, but the array elements cannot be repeated. Here, the length of the array is not specified for us, we can let it be any length between 1-100.
Next let's take a look at how to implement it better. Usually we will use ArrayList to implement it, as shown in the following code:
Copy the code code as follows:
package cn.sunzn.randomnumber;
import java.util.ArrayList;
import java.util.Random;
public class Demo {
public static void main(String[] args) {
Object[] values = new Object[20];
Random random = new Random();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < values.length; i++) {
int number = random.nextInt(100) + 1;
if (!list.contains(number)) {
list.add(number);
}
}
values = list.toArray();
/************ Traverse the array and print the data************/
for (int i = 0; i < values.length; i++) {
System.out.print(values[i] + "/t");
if ((i + 1) % 10 == 0) {
System.out.println("/n");
}
}
}
}
The above implementation process is relatively inefficient. Because every time you add it, you have to traverse whether the number exists in the current list, the time complexity is O(N^2). We can think about it this way: Since it involves no duplication, we can think about the functions of HashSet and HashMap. HashSet implements the Set interface. The mathematical definition of Set is a collection without duplication and order. HashMap implements Map and does not allow duplicate Keys. In this way we can use HashMap or HashSet to achieve this.
When using HashMap to implement it, you only need to convert its key into an array. The code is as follows:
Copy the code code as follows:
package cn.sunzn.randomnumber;
import java.util.HashMap;
import java.util.Random;
public class Demo {
public static void main(String[] args) {
Object[] values = new Object[20];
Random random = new Random();
HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
/******* Generate random numbers and store them in HashMap *******/
for (int i = 0; i < values.length; i++) {
int number = random.nextInt(100) + 1;
hashMap.put(number, i);
}
/************ Import array from HashMap************/
values = hashMap.keySet().toArray();
/*********** Traverse the array and print the data*************/
for (int i = 0; i < values.length; i++) {
System.out.print(values[i] + "/t");
if ((i + 1) % 10 == 0) {
System.out.println("/n");
}
}
}
}
Since the relationship between HashSet and HashMap is too close, HashSet is implemented using HashMap at the bottom layer. However, there is no collection of Values, only a collection of Keys, so it can also be implemented using HashSet. The code is as follows:
Copy the code code as follows:
package cn.sunzn.randomnumber;
import java.util.HashSet;
import java.util.Random;
public class Demo {
public static void main(String[] args) {
Random random = new Random();
Object[] values = new Object[20];
HashSet<Integer> hashSet = new HashSet<Integer>();
/******* Generate random numbers and store them in HashSet *******/
for (int i = 0; i < values.length; i++) {
int number = random.nextInt(100) + 1;
hashSet.add(number);
}
values = hashSet.toArray();
/************* Traverse the array and print the data**********/
for (int i = 0; i < values.length; i++) {
System.out.print(values[i] + "/t");
if ((i + 1) % 10 == 0) {
System.out.println("/n");
}
}
}
}
This implementation is slightly more efficient. If we limit the length of the array, we only need to change the for loop and set it to a whlie loop. As shown below:
Copy the code code as follows:
package cn.sunzn.randomnumber;
import java.util.HashSet;
import java.util.Random;
public class Demo {
public static void main(String[] args) {
Random random = new Random();
Object[] values = new Object[20];
HashSet<Integer> hashSet = new HashSet<Integer>();
/****** Generate random numbers and store them in HashSet ******/
while (hashSet.size() < values.length) {
hashSet.add(random.nextInt(100) + 1);
}
values = hashSet.toArray();
/************ Traverse the array and print the data************/
for (int i = 0; i < values.length; i++) {
System.out.print(values[i] + "/t");
if ((i + 1) % 10 == 0) {
System.out.println("/n");
}
}
}
}