0

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

  • 3
    "how would I assign values depending on the type of the array I want to build" You need to pass in a `Function` to create the array, and a `Function` to parse the string to a `T`. – Andy Turner Aug 31 '17 at 12:31
  • 2
    Scanner.next always returns a string http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/util/Scanner.java#line-1349 so this generics would fail, because the type always needs to be a type of string. – Tschallacka Aug 31 '17 at 12:38
  • @nathan I deleted my answer because as you pointed out I didn't understand the question – rakwaht Aug 31 '17 at 12:47
  • hey @AndyTurner can you plz give the code,(or change my code) that will be very helpful thanks –  Aug 31 '17 at 12:52
  • Possible duplicate of [How to create a generic array in Java?](https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – Gourav Sep 01 '17 at 06:05

2 Answers2

0

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;
}

}

Gourav
  • 35
  • 1
  • 9
  • you have to return the complete array and you can't create a generic type of array so you need to create an array of objects and then typecast it to generics. I hope this works – Gourav Sep 01 '17 at 05:32
  • it will, except that there will be no runtime type checking on the element type. – Maurice Perry Sep 01 '17 at 05:43
  • I did not add any checking as there is no **demand** for it in the question – Gourav Sep 01 '17 at 05:51
  • What I meant, is that you will be able to put any type of object in the array, whereas had the array been created with the correct element type, this would not be possible. – Maurice Perry Sep 01 '17 at 05:53
0

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);
    }
Maurice Perry
  • 7,501
  • 2
  • 9
  • 19