Answer the question
In order to leave comments, you need to log in
How to write an int value to an array of type Integer?
First of all, I create an interface.
public interface MyRandom <T> {
public T GetRandom ();
}
import java.util.*;
public class Group {
@SuppressWarnings("rawtypes")
public static class Char implements MyRandom <String> {
private Random rand = new Random ();
private String [] arr = "qwertyuiopasdfghjklzxcvbnm".split("");
public String GetRandom() {
return arr[rand.nextInt(arr.length)];
}
}
@SuppressWarnings("rawtypes")
public static class Integer implements MyRandom <Integer> {
private Random rand = new Random ();
public Integer GetRandom() {
return rand.nextInt(10);
}
}
}
public class MainClass {
public static <T> T[] FillArr (T [] arr, MyRandom <T> generator){
for (int i=0; i!=arr.length; i++)
arr[i] = generator.GetRandom();
return arr;
}
public static void main(String[] args) {
String [] StringArr = FillArr (new String[10], new Group.Char());
for (String s : StringArr){
System.out.println(s);
}
}
}
Answer the question
In order to leave comments, you need to log in
public static class Integer implements MyRandom <java.lang.Integer> {
private Random rand = new Random();
public java.lang.Integer GetRandom() {
return rand.nextInt(10);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question