Take an array input using a generic function in java

1.8k views Asked by At

what I am trying to do is, I have a UTILITY package and I wanted to make a class GetArray with a method get, which creates an array and return that array, also I wanted to make this method generic so I could create what ever type of array I wanted to.

The problem is in the statement arr[i] = in.next(); in the loop. ie, how would I assign values depending on the type of the array I want to build

public class GetArray {

    /**
     * @param takes a scanner varable
     * @return returns an array of all the elements you specify
     */

    public static <T> int[] get(Scanner in) {

        System.out.print("enter array size :  ");
        int ar_size = in.nextInt();

        System.out.print("arr elements: ");
        T arr[] = new T[ar_size];

        for (int i = 0; i < ar_size; i++) {
            arr[i] = in.next();
        }
        return arr;
    }
}

I will be calling this method from my main.java, and therefore I am passing the scanner to it

2

There are 2 answers

0
Maurice Perry On

To have the correct array type, you need to pass the class of the elements:

public static <T> T[] get(Scanner in, Class<T> clazz) {

    System.out.print("enter array size :  ");
    int ar_size = in.nextInt();

    System.out.print("arr elements: ");
    T arr[] = (T[])Array.newInstance(clazz, ar_size);

    for (int i = 0; i < ar_size; i++) {
        arr[i] = clazz.cast(in.next());
    }
    return arr;
}

UPDATE: but Scanner.next always returns a String, so I'm afraid You'll have to test the class to know which method of Scanner to use:

    for (int i = 0; i < ar_size; i++) {
        Object elem = null;
        if (clazz == Byte.class) {
            elem = in.nextByte();
        } else if (clazz == Short.class) {
            elem = in.nextShort();
        } else if (clazz == Integer.class) {
            elem = in.nextInt();
        } else if (clazz == Long.class) {
            elem = in.nextLong();
        } else if (clazz == Float.class) {
            elem = in.nextFloat();
        } else if (clazz == Double.class) {
            elem = in.nextDouble();
        } else if (clazz == BigInteger.class) {
            elem = in.nextBigInteger();
        } else if (clazz == BigDecimal.class) {
            elem = in.nextBigDecimal();
        } else if (clazz == Boolean.class) {
            elem = in.nextBoolean();
        } else if (clazz == String.class) {
            elem = in.next();
        }
        arr[i] = clazz.cast(elem);
    }
4
Gourav On

if you want to have generic array then you have to modify your code like this

      public class GetArray {

/**
 * @param takes a scanner varable
 * @return returns an array of all the elements you specify
 */

@SuppressWarnings("unchecked")
public static <T> T[] get(Scanner in) {

    System.out.print("enter array size :  ");
    int ar_size = in.nextInt();

    System.out.print("arr elements: ");
    Object arr[] =  new Object[ar_size];

    for (int i = 0; i < ar_size; i++) {
        arr[i] =  (T)in.next();
    }
    return (T[]) arr;
}

}