0

I have this implementation of an array handler where it takes an array and adds a new value of the array.

at the moment I have it working for long type.

I want it to work for any type, if that is possible.

current implementation :

public class ArrayHandler 
{
    public static long[] add(long[] arr, long value)
    {
        long[] newarr = new long[arr.length+1];
        if(arr.length == 0)
        {
            newarr[0] = value;
        }
        else
        {
            for(int i = 0 ; i < arr.length ; i++)
            {
                newarr[i] = arr[i];
            }
        }
        newarr[arr.length] = value;
        return newarr;
    }
}
Moe
  • 31
  • 1
  • 1
  • 6
  • For primitive types, you can't, you'll need to copy/paste. – Louis Wasserman Feb 11 '15 at 23:24
  • what do you mean reading tutorial ? @KickButtowski is there a tutorial you recommend as I tried to look everywhere possible and nothing is there – Moe Feb 11 '15 at 23:24
  • Google generic Java tutorial . – Kick Buttowski Feb 11 '15 at 23:25
  • so it is not possible ? @LouisWasserman – Moe Feb 11 '15 at 23:26
  • 4
    Nope, not for primitive types. You can copy/paste to generate overloads for every primitive type, and you can build a generic implementation for reference array types. – Louis Wasserman Feb 11 '15 at 23:27
  • 2
    As a side note: you can remove the `if` branch, it is not necessary. If `arr` is empty, then the loop will be skipped and `newarr[arr.length] = value;` inserts the value on the first index. – Tom Feb 11 '15 at 23:30
  • You can avoid a lot of the copy & paste by [reading the API docs](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOf-boolean:A-int-). – vanza Feb 12 '15 at 03:21

2 Answers2

0

I hope this "workaround" is good enough for you:

public class Test {

    public static void main(String[] args) {
        Long theLong = new Long(1234);
        Integer theInteger = new Integer(421);

        Number[] myArray = null;

        myArray = add(myArray, theLong);
        myArray = add(myArray, theInteger);
        System.out.println(myArray[0]);
        System.out.println(myArray[1]);
    }

    public static Number[] add(Number[] arr, Number value) {
        Number[] newarr = null;
        if (arr == null || arr.length == 0) {
            newarr = new Number[1];
            newarr[0] = value;
        } else {
            for (int i = 0; i < arr.length; i++) {
                newarr = new Number[arr.length + 1];
                newarr[i] = arr[i];
            }
            newarr[arr.length] = value;
        }
        return newarr;
    }

}
vianna77
  • 455
  • 1
  • 6
  • 17
  • Problem with this is it introduces type boxing, which has reduced performance when compared to primitive types. – Lesleh Feb 12 '15 at 03:15
  • There are other problems... If performance was really an issue to be considered, maybe this array handler should be reviewed and rewritten. There is some space to improve the code. I was just thinking in a way to let the person work with anything he/she wants. – vianna77 Feb 12 '15 at 03:22
0

You can have a single method that works for all types of arrays using reflection, but you will have to lose type safety (the only supertype of primitive array types and reference array types is Object):

import java.lang.reflect.Array;
public class ArrayHandler 
{
    public static Object add(Object arr, Object value)
    {
        Object newarr = Array.newInstance(arr.getClass().getComponentType(),
                                          Array.getLength(arr)+1);
        System.arraycopy(arr, 0, newarr, 0, Array.getLength(arr));
        Array.set(newarr, Array.getLength(arr), value);
        return newarr;
    }
}
newacct
  • 110,405
  • 27
  • 152
  • 217