Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JAVA_ADVANCED.lec_17_Generics.Task2_ArrayCreater;
- import java.lang.reflect.Array;
- import java.util.Arrays;
- public class ArrayCreator {
- public static <T> T[] create(int length, T item) {
- T[] array = (T[]) Array.newInstance(item.getClass(), length);
- for (int i = 0; i < length; i++) {
- array[i] = item;
- }
- return array;
- }
- public static <T> T[] create(Class<T> clazz, int length, T item) {
- T[] array = (T[]) Array.newInstance(clazz, length);
- for (int i = 0; i <length ; i++) {
- array[i]=item;
- }
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement