You can do it using nested loop, as you said in the question:
public String[] randomArrayString(int length, int numberOfChar) {
Random random = new Random();
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
String[] array = new String[length];
String str;
for (int i = 0; i < length; i++) {
str = "";
for (int j = 0; j < numberOfChar; j++) {
str+= chars[random.nextInt(chars.length - 1)];
}
array[i] = str;
}
return array;
}
You can make a solution also using Java 8 Streams, like this:
public Object[] randomArrayString(int length, int numberOfChar){
Random random = new Random();
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
return Stream.generate(() -> {
String str="";
for (int i = 0; i < numberOfChar; i++) {
str+= chars[random.nextInt(chars.length - 1)];
}
return str;
}).limit(length).toArray();
}
3
solved Generating a Random String Array