Creating a Generic Array in Java Last Updated : 02 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Arrays in Java are generated using a certain data type. On the other hand, you may create a generic array that functions with various object types by utilizing generics. You can build type-safe, reusable code by using generics. In this article, we will learn about creating generic arrays in Java.Java's generics let you write methods, interfaces, and classes that take in and use many kinds of arguments. Usually, while creating the class or function, you'll utilize the generic type to construct a generic array.Example 1: Generic Class with Array Java //Driver Code Starts // Implement Generic Array //Driver Code Ends // GenericArray Class class GenericArray<T> { private T[] arr; // Constructor Creating Array public GenericArray(int s) { arr = (T[]) new Object[s]; } // Setting Element in Array public void setElement(int i, T val) { arr[i] = val; } // Getting Element from Array public T getElement(int i) { return arr[i]; } public static void main(String[] args) //Driver Code Starts { // Creating Generic Array GenericArray<String> arr = new GenericArray<>(5); arr.setElement(0, "Java"); arr.setElement(1, "is"); arr.setElement(2, "awesome"); System.out.println("Element at index 1: " + arr.getElement(1)); } } //Driver Code Ends OutputElement at index 1: is Example 2: Generic Method with Array Java import java.lang.reflect.Array; import java.util.Arrays; class GenericArrayExample { @SuppressWarnings("unchecked") public static <T> T[] createArray(int size, T defaultValue) { // Creating a generic array using reflection T[] array = (T[]) Array.newInstance(defaultValue.getClass(), size); Arrays.fill(array, defaultValue); return array; } // Driver Method public static void main(String[] args) { // Generic Array Integer Type Integer[] intArray = createArray(5, 0); System.out.println("Integer Array: " + Arrays.toString(intArray)); // Generic Array String Type String[] strArray = createArray(3, "Hello"); System.out.println("String Array: " + Arrays.toString(strArray)); } } OutputInteger Array: [0, 0, 0, 0, 0] String Array: [Hello, Hello, Hello] Comment More infoAdvertise with us Next Article Java Program to Create ArrayList From Enumeration R ravi86526iv Follow Improve Article Tags : Java Java Programs Java-Arrays Java Examples Practice Tags : Java Similar Reads Dynamic Array in Java Arrays are linear data structures, and similar types of elements will be inserted in continuous memory locations. Now as we know there is an issue with arrays that size needs to be specified at the time of declaration or taken from the user in Java. Hence, there arise dynamic arrays in Java in which 3 min read Implementing Associate Array in Java An associative array stores the set of elements in the form of (key, value) pairs. An associative array is a collection of unique keys and collections of values where each key is associated with one value. An associate array is an abstract datatype like a map that is composed of a (key, value) pair, 3 min read Immutable Array in Java In this article, we will talk about how to implement an immutable Array in Java. Immutable means we cannot modify it. If you have an immutable array in Java means refers to an array whose content and size cannot be changed after it has been initialized. How to Implement an Immutable Array in Java?So 2 min read Java Program to Create ArrayList From Enumeration Enumerations serve the purpose of representing a group of named constants in a programming language. Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay 2 min read Creating a User-Defined Printable Pair Class in Java The pair class in C++ Standard Library is used a lot. We can implement our own user-defined pair class in Java and its object can be used anywhere just like any other parameter. Note : This class is equivalent to pair<int,int> class in java. You can create your own template or classes for othe 3 min read Java Program to Change a Collection to an Array An array is a data structure that can hold a fixed-size, homogeneous collection of elements of the same data type, which can be either primitive data types (e.g., int, float) or object references. However, the size of the array cannot be changed once it is created. On the other hand, a collection is 3 min read Like