How do i create generic array in Java

0 votes

Due to the implementation of Java generics, you can't have code like this:

public class GenericSet<E> {
    private E a[];

    public GenericSet() {
        a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation
    }
}

How can I implement this while maintaining type safety?

One way to do it is :

import java.lang.reflect.Array;

class Stack<T> {
    public Stack(Class<T> clazz, int capacity) {
        array = (T[])Array.newInstance(clazz, capacity);
    }

    private final T[] array;
}

But it is very confusing to  understand

May 15, 2018 in Java by sharth
• 3,370 points
17,225 views

2 answers to this question.

0 votes

Generic array creation is not allowed in Java.

But, this might be a good solution to this unavailability :

class Stack<T> {
private final T[] array;
public Stack(int capacity) {
    array = (T[]) new Object[capacity];
 }
}

Hope this helps!!

If you need to know more about Java, join our Java certification course online.

Thanks!!

answered May 15, 2018 by Parth
• 4,640 points
how you will assign value to final variables ?
0 votes
as first tip you cannot assign value to the final variables

and also you need to declare your method return type
answered Feb 13, 2020 by sama

Related Questions In Java

0 votes
1 answer

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,770 points
1,247 views
0 votes
0 answers

How do I declare and initialize an array in Java?

How do I declare and initialize an ...READ MORE

Feb 8, 2022 in Java by Edureka
• 120 points
400 views
0 votes
1 answer

How do I declare and initialize an array in Java?

To declare and initialize an array in ...READ MORE

answered Jun 22, 2023 in Java by Khan Sarfaraz
• 700 points
451 views
0 votes
0 answers

How do I declare and initialize an array in Java?

How can I declare and initialize an ...READ MORE

Aug 11, 2022 in Java by krishna
• 2,820 points
451 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,326 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,185 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,578 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,220 points
993 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by Parth
• 4,640 points
1,210 views
0 votes
2 answers

How can I get current time in YYYY:MM:DD HH:MI:Sec:Millisecond format in Java?

public String getCurrentTimeStamp() { ...READ MORE

answered Sep 21, 2018 in Java by Parth
• 4,640 points
7,510 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP