3

Here's what I am trying to do:

public void add(Integer... newIntegers) {
    for (Integer i : newIntegers) {
        integers.add(i);
    }
}

I am not sure why, but I can't put an ArrayList<Integer> into the method.

I am more than happy to just copy the method for Lists but I thought that there had to be a more efficient way.

I tried the following thinking that I could do what you can do for a catch block:

public void add(Integer... | List<Integer> newIntegers) {
    for (Integer i : newIntegers) {
        integers.add(i);
    }
}

However, after a quick StackOverfow, found it was impossible. Is there any other way?

TheChubbyPanda
  • 1,721
  • 2
  • 16
  • 37

2 Answers2

5

A method accepting Integer... cannot consume a collection. it can only consume an array of Integer's.

The best you can do is use method overloading.

public void add(Integer...newIntegers) {
    for (Integer i : newIntegers) {
        integers.add(i);
    }
}

public void add(List<Integer> newIntegers) {
    for (Integer i : newIntegers) {
        integers.add(i);
    }
}

Personally, I'd stick with the above but if you want to avoid code duplication and instead construct a new list every time the public void add(Integer...newIntegers) {...} is called then you can do as @Sweeper has suggested in the comments.

public void add(Integer... newIntegers) {
     add(Arrays.asList(newIntegers));
}

public void add(List<Integer> newIntegers) {
    for (Integer i : newIntegers) {
        integers.add(i);
    }
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • I think the OP is trying to avoid repetition. Quote: "I am more than happy to just copy the method for Lists but I thought that there had to be a more efficient way." – Sweeper Apr 22 '18 at 13:14
  • @Sweeper is there a more efficient way? – Ousmane D. Apr 22 '18 at 13:15
  • @Sweeper Thank's for that. Bang on. – TheChubbyPanda Apr 22 '18 at 13:16
  • I was thinking that OP could create an `add(List)` and have the `add(Integer...)` overload call that by converting the integer array to a list. Imagine if the method were way longer than this. – Sweeper Apr 22 '18 at 13:17
  • Really? I'm surprised that it's not a feature since both ways return an Iterable instance. – TheChubbyPanda Apr 22 '18 at 13:18
  • 1
    `Iterable` is an interface in Java. Anything that implements `Iterable` can be put in an enhanced `for` loop, but just because an array can be put into an enhanced `for` loop as well, doesn't make it `Iterable`. They are two different things: one is an array, the other isn't. – SeverityOne Apr 22 '18 at 13:21
  • Oh well. Accepted then. – TheChubbyPanda Apr 22 '18 at 13:22
  • @Sweeper well you could do that to avoid _code duplication_ but now you're constructing a new list with all the elements from the array. surely that is not more efficient than what's shown above. Anyway, I will provide a solution for that and let the OP decide what they want. – Ousmane D. Apr 22 '18 at 13:26
  • Related discussion: https://stackoverflow.com/questions/1160081/why-is-an-array-not-assignable-to-iterable – Patrick Parker Apr 22 '18 at 13:32
  • @Aominè Arrays.asList is pretty efficient; it doesn't clone the array or copy its contents. Of course there is a small cost to creating any object. – Patrick Parker Apr 22 '18 at 14:07
  • @TheChubbyPanda while your edit is certainly true it's more efficient to do it the way I have shown. See the comment Andy Turner has left under the other answer _creating a list from an array is an o(1) operation; creating an array from a list is o(size)._. hence I've recommended proceeding with the above approach. – Ousmane D. Apr 22 '18 at 14:10
2

You can overload it by doing the below code, it will let you reuse the Add method previously created.

public void add(Integer...newIntegers) {
    for (Integer i : newIntegers) {
        integers.add(i);
    }
}

public void add(List<Integer> newIntegers) {
   add(newIntegers.stream().toArray(Integer[]::new));
}
Akhadra
  • 419
  • 3
  • 10
  • 3
    You shouldn't do it this way round: creating a list from an array is an o(1) operation; creating an array from a list is o(size). – Andy Turner Apr 22 '18 at 13:35