Javatpoint Logo
Javatpoint Logo

Java Array Generic

Arrays in Java are basic data structures used to store and manipulate collections of objects of the same type. The limitation of arrays in Java, however, is that they inherently cannot store objects. This limitation can be overcome by using a normal setting. Java introduced generics in Java 5 (also known as Java SE 5) to provide compile time type protection for collections. The concept of generics allows us to define classes, interfaces, and methods that can operate on a variety of data types while maintaining type safety. Generics enable us to create parameterized types, which are classes or interfaces that can be used with different data types. When it comes to arrays, generics allow you to create arrays that can store any type of element. We can use the type parameter feature of generics to do this. Here is an example of how to create a generic array in Java:

Output:

Hello World
60

Explanation:

  1. The GenericArray<String> object stringArray is created with size 5 .
  2. The set() method is used to set the values at indexes 0 and 1 of the stringArray with the strings "Hello" and "World".
  3. The get() method retrieves the value from indices 0 and 1 of the stringArray.
  4. The concatenated string str variable is assigned "Hello" and "World".
  5. The str value is printed, resulting in: Hello World.
  6. The GenericArray<Integer> object intArray is created with size 3 .
  7. The set() method is used to set values at indices 0, 1, and 2 of an intArray of 10, 20, and 30 integers, respectively.
  8. The get() method retrieves the values of indices 0, 1, and 2 of the intArray.
  9. The values are summed and assigned to all variables.
  10. The cumulative value is printed, producing the result: 60 .
  • Array Creation and Type Deletion:

If we are creating a generic array, you will get a warning about unchecked or unsafe functions due to type deletion. Type erasure is the process in Java where type parameters are replaced with their upper bounds or deleted entirely to ensure compatibility with pre-generics code. In the example code given earlier, the line (T[]) throws another unsigned Object[size]. Compiling this code can cause a runtime error if the array is incorrectly typed.

  • Limitations of generic arrays:

Generic arrays in Java have a few limitations due to type erasure. One important limitation is that you cannot create arrays of concrete generic types directly. For example, the following code will generate a compile-time error:

To overcome this limitation, we can write a program Objects and cast them into generic types as shown in the previous example.

  • Wildcard and restricted type parameters:

When working with normal arrays, you can also use wildcard and bounded type parameters. Wildcards allow you to work with unknown types or create more flexible generic arrays. For example, we can define a generic array that accepts any subtype of a particular class or interface:

  • Generic array advantages:

Java provides a variety of utility classes and methods for working with arrays, including generic arrays. The java.util.Arrays class provides several static methods for modifying and searching arrays. These methods can also be used in normal arrays. For example, the sort() method can be used to sort a generic array in ascending order.

Generic arrays in Java enable flexible and reusable data structures that can handle a variety of types. However, due to type erasure, it is important to be careful when working with generic arrays to avoid runtime errors, and to ensure proper type checking.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA