@thatmaniscool

Как в массив типа Integer записать значение типа int?

Прежде всего я создаю интерфейс.
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);
		}

	}

}
  • Вопрос задан
  • 187 просмотров
Решения вопроса 1
public static class Integer implements MyRandom <java.lang.Integer> {
        private Random rand = new Random();

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

Сделать вот так

Ну или переименовать свой класс из Integer во что-то другое
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы