T
T
thatmaniscool2017-06-05 19:19:01
Java
thatmaniscool, 2017-06-05 19:19:01

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 ();
}

Then I create a class with two subclasses. The first is string and the second is numeric. String works, numeric does not. What needs to be done to return a numeric value?
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);
    }
  }
}

Well, actually the entry point
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

1 answer(s)
A
Alexander Oparin, 2017-06-05
@thatmaniscool

public static class Integer implements MyRandom <java.lang.Integer> {
        private Random rand = new Random();

        public java.lang.Integer GetRandom() {
            return rand.nextInt(10);
        }
    }

Do like this
Well, or rename your class from Integer to something else

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question