Advertisement
Georgi_Benchev

Untitled

Oct 25th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. package JAVA_ADVANCED.lec_17_Generics.Task2_ArrayCreater;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.Arrays;
  5.  
  6. public class ArrayCreator {
  7.     public static <T> T[] create(int length, T item) {
  8.         T[] array = (T[]) Array.newInstance(item.getClass(), length);
  9.         for (int i = 0; i < length; i++) {
  10.             array[i] = item;
  11.         }
  12.         return array;
  13.     }
  14.  
  15.     public static <T> T[] create(Class<T> clazz, int length, T item) {
  16.         T[] array = (T[]) Array.newInstance(clazz, length);
  17.         for (int i = 0; i <length ; i++) {
  18.             array[i]=item;
  19.         }
  20.         return array;
  21.     }
  22.  
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement