所以我有一系列这样的名字:
String[] arrayOfName = {"Elephant", "Deer", "Fish", "Crocodile", "Cat", "Dog", "Bird", "Butterfly", "Chicken", "Ant", "Snake", "Lion", "Horse", "Wolf", "Panda", "Swan", "Lobster"};
我在考虑是否有另一个数组,我想将arrayOfName
中的4到10个(它是随机的)随机名称放入我的新数组中。
答案 0 :(得分:1)
尝试使用Random
类:
import java.util.Random;
public class Test {
public static void main (String[] args) {
Random rand = new Random();
String[] arrayOfName = {"Elephant", "Deer", "Fish", "Crocodile", "Cat", "Dog", "Bird", "Butterfly", "Chicken", "Ant", "Snake", "Lion", "Horse", "Wolf", "Panda", "Swan", "Lobster"};
// this will give you a random value between 4 (inclusive) and 11 (exclusive, or 10 inclusive)
int numberOfRandomValues = rand.nextInt(7) + 4;
String[] newArrayOfName = new String[numberOfRandomValues];
for(int i = 0; i < numberOfRandomValues; i++) {
// this will give you a random value between 0 (inclusive) and the size of array (inclusive as well) which you can use to add a random element
int randomPosition = rand.nextInt(arrayOfName.length);
newArrayOfName[i] = arrayOfName[randomPosition];
System.out.println(" - Just added " + newArrayOfName[i] + " on position " + i);
}
}
}
答案 1 :(得分:1)
第一步:您可以使用Random对象并生成4到10之间的数字。
Random random = new Random();
int minElementToTake = 4;
int maxElementsToTake = 10;
int nbElementToTake = random.nextInt(maxElementsToTake - minElementToTake + 1) + minElementToTake;
此处random.nextInt()
生成0到6之间的数字。然后,我们将4添加到其中
因此int nbElementToTake
为>= 4 && <=10
。
第二步:迭代“nbElementToTake
”次并使用nextInt()方法生成介于0和原始数组大小之间的int。
通过这种方式,您将拥有一个表示原始数组的随机索引的int,您可以从中检索一个随机元素,以将其存储在目标数组中。
注意:在提出的解决方案中,我添加了一个Set以避免在目标数组中多次添加相同的值。它不是处理这种问题的有效方法,但对于数据量不大的数组,这是可以接受的 通过使用List而不是数组,我们可以为此问题提供更高效,更简单的解决方案。
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class AnimalRandom {
public static void main(String[] args) {
String[] arrayOfName = { "Elephant", "Deer", "Fish", "Crocodile", "Cat", "Dog", "Bird", "Butterfly", "Chicken",
"Ant", "Snake", "Lion", "Horse", "Wolf", "Panda", "Swan", "Lobster" };
Random random = new Random();
int minElementToTake = 4;
int maxElementsToTake = 10;
int nbElementToTake = random.nextInt(maxElementsToTake - minElementToTake + 1) + minElementToTake;
String[] newArray = new String[nbElementToTake];
Set<Integer> indexesUsed = new HashSet<>();
Random randomOrder = new Random();
for (int i = 0; i < nbElementToTake; i++) {
int maxInclusive = arrayOfName.length;
int indexToUse = randomOrder.nextInt(maxInclusive);
while (indexesUsed.contains(indexToUse)) {
indexToUse = randomOrder.nextInt(maxInclusive);
}
indexesUsed.add(indexToUse);
newArray[i] = arrayOfName[indexToUse];
}
System.out.println(Arrays.toString(newArray));
}
}
答案 2 :(得分:0)
虽然没有经过测试,但想出了这个要求的存根 -
/**
* To pick any few sequential input strings from the list of inputs
* @param inputArr input array of strings
* @param few number of strings to be picked up
* @return any random sequence of x strings from input
*/
public static String[] randomSequenceOfFewStrings(String[] inputArr, int few) {
String[] outputArr = new String[few];
Random random = new Random();
int index = random.nextInt(inputArr.length - few);
for (int i = 0; i < few; i++) {
outputArr[i] = inputArr[index];
index++;
}
return outputArr;
}
其中您可以将相同的代码用作 -
System.out.println(Arrays.toString(randomSequenceOfFewStrings(arrayOfName, 5)));
注意 - 这应该为每个具有相同参数的调用生成不同的固定数量的元素序列,并为每个不同的方法调用生成不同数量的元素的不同序列。
答案 3 :(得分:0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Example1 {
public static void main(String[]args){
String[] arrayOfName = {"Elephant", "Deer", "Fish", "Crocodile", "Cat", "Dog", "Bird", "Butterfly", "Chicken", "Ant", "Snake", "Lion", "Horse", "Wolf", "Panda", "Swan", "Lobster"};
List<String> listOfNames = new ArrayList<>(Arrays.asList(arrayOfName)); // convert array to list
Collections.shuffle(listOfNames); // shuffle the list
Random ran = new Random();
List<String> listOfRandomNames = new ArrayList<>(listOfNames.subList(0, 4 + ran.nextInt(6))); // pick random number of strings from the shuffled list
String[] arrayOfRandomNames = new String[listOfRandomNames.size()]; // new Array with size of random list
arrayOfRandomNames = listOfRandomNames.toArray(arrayOfRandomNames); // convert list to array
System.out.println(Arrays.toString(arrayOfRandomNames)); // print or do whatever
}
}