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?

3
  • You can convert your list before you call your method there are many ways to do that Apr 22, 2018 at 13:26
  • Or convert the varargs to a list with List.of(E...) and then call the add(List<Integer>) implementation.
    – Turing85
    Apr 22, 2018 at 13:27
  • 1
    integerList.add(otherIntegerList.toArray(new Integer[]{})); Maybe not efficient or pretty but it works. Apr 22, 2018 at 13:37

2 Answers 2

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);
    }
}
11
  • 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, 2018 at 13:14
  • @Sweeper is there a more efficient way?
    – Ousmane D.
    Apr 22, 2018 at 13:15
  • @Sweeper Thank's for that. Bang on. Apr 22, 2018 at 13:16
  • I was thinking that OP could create an add(List<Integer>) 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, 2018 at 13:17
  • 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. Apr 22, 2018 at 13:21
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));
}
1
  • 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). Apr 22, 2018 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.