-3
public void expand(???, int number) {

    array = java.util.Arrays.copyOf(array, array.length + number);

}

In "???", I want to accept any array regardless of type. Does the solution lie in Generic types?

Jongware
  • 21,058
  • 8
  • 43
  • 86
  • Depends what you want to do. Arrays typically don't work very well with generics because you cannot allocate a generic array. You can however use some static methods on `Array` to instantiate such an array – Dici Sep 04 '15 at 23:41
  • The method's only purpose is to take an array and increase its length using copyOf. I've used copyOf before with arrays made from objects that I've made myself, so I know that copyOf would be able to accept any array, regardless of type. I would like that same flexibility in this expand method. – SusuKacangSoya Sep 04 '15 at 23:46
  • If all it does is call `copyOf`, why can't caller do that? BTW: `copyOf` is **10** different methods. – Andreas Sep 05 '15 at 00:03
  • @Andreas I'm aware. The correct copyOf is picked from the argument's type. But I've seen copyOf work on newly created types too. Also, this method was meant to be for convenience, or at least laziness. – SusuKacangSoya Sep 05 '15 at 00:11
  • @MickMnemonic In this case, I suppose that is a solution too. Thanks. I initially wanted to pass an int[], so the Object[] type wouldn't work. – SusuKacangSoya Sep 05 '15 at 00:12
  • 1
    @SusuKacangSoya FYI: You don't update the question with the answer, you mark the appropriate answer as accepted by clicking the checkmark. If you end up solving it yourself, you answer your own question and accept that. – Andreas Sep 05 '15 at 00:24
  • Gotcha. Thanks guys, I'm new to StackOverflow. Appreciate the help from all of you. I'll keep things in mind. – SusuKacangSoya Sep 05 '15 at 00:37
  • I'm voting to close this question as off-topic because , problem appears to be 'solved' – Jayan Sep 05 '15 at 10:58

2 Answers2

1

If by "any array" you mean both an array of objects (e.g. String[]) and an array of primitives (e.g. int[]), then generics won't help you, and your only option is to take an Object and verify it's an array.

public void expand(Object array, int number) {
    if (! array.getClass().isArray())
        throw new IllegalArgumentException("Not an array");
    int arrayLen = Array.getLength(array);

    // Sample methods for accessing array
    Class<?> arrayValueClass = array.getClass().getComponentType(); // E.g. int.TYPE
    int firstInt = Array.getInt(array, 0); // Assumes array is int[]
    Object firstVal = Array.get(array, 0); // Always works. Primitives are boxed
}

Update

But, since you just want a convenience method for calling copyOf, your best solution is to implement 10 versions of expand, one for each version of copyOf, less if you don't need support for all primitive array types:

public <T> T[] expand(T[] array, int number) {
    return Arrays.copyOf(array, array.length + number);
}
public int[] expand(int[] array, int number) {
    return Arrays.copyOf(array, array.length + number);
}
public double[] expand(double[] array, int number) {
    return Arrays.copyOf(array, array.length + number);
}
// and so on...
Andreas
  • 138,167
  • 8
  • 112
  • 195
0
Integer[] array = {1,2,3};
array = expand(array, 10);
System.out.println(array.length);

public <T> T[] expand(T[] array, int number) {
    array = java.util.Arrays.copyOf(array, array.length + number);
    return array;
}