Creating a Generic Array in Java Last Updated : 02 Jan, 2025 Comments Improve Suggest changes 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 Creating a Generic Array in Java 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 Convert HashSet to array in Java Java HashSet class is used to create a collection that uses a hash table for the storage of elements. It inherits AbstractSet class and implements Set Interface. The key points about HashSet are: HashSet contains unique elements only.HashSet allows null values.The insertion of elements in a HashSet 2 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 How Objects Can an ArrayList Hold in Java? ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. In order to understan 3 min read How to Fill (initialize at once) an Array in Java? An array is a group of like-typed variables that are referred to by a common name. In this, article we will learn about Filling array in Java while Initialization.Example:Java// Java program to fill the element in an array import java.util.*; public class Geeks { public static void main(String args[ 3 min read Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r 4 min read Like