2

I am a List of Long type and would like to convert it to an array of long type.

However when adding elements to the list, it says it is unable to find method "add(int)"

my code looks like below

package collections;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class ListToArray {

    private static List<Long> list;

    public static void main(String[] args){

        list=new ArrayList<Long>();
        list.add(121145788);
        list.add(1245898);

        long[] arr=new long[list.size()];

        arr=list.toArray();

    }
}

Error I am getting are

Error(16,13): cannot find method add(int). Error(21,25): incompatible types

Can someone point out where it's going wrong.

Rob
  • 26,989
  • 16
  • 82
  • 98
redsoxlost
  • 1,215
  • 5
  • 19
  • 32
  • what java version are you using? – ItamarG3 May 05 '18 at 15:59
  • It shouldn't necessary, but trying postfixing the numbers with L to make them long literals: `121145788L`. I would expect that it could coerce it without your intervention though. – Carcigenicate May 05 '18 at 16:00
  • Why did you edit the code in the question? As it is now, it won't give you the error anymore. In its current state, the question should be closed because the problem can't be reproduced .... – Robin Topper May 05 '18 at 17:01

4 Answers4

4

As suggested in the comments, you should add L to force the value to be a long:

list.add(121145788L);
list.add(1245898L);

Explanation:

the values you wanted to add are ints, and list is for longs. Adding the L makes the compiler use the number literal as a value of type Long explicitly, and that can be added to the list.

Without it, the numbers are treated as Integers, and those can't be added to a list of type Long, hence

cannot find method add(int)

There is a method add(long), and that's what is used when the L is used.

To convert to an array, you need to do it the old fashioned way:

for (int i = 0; i < arr.length; i++) {
    arr[i]=list.get(i);
}

This is because long is a primitive, and Long is a reference type (i.e. it is an Object). Casting from primitives to Objects isn't always easy. In this case, the old fashioned way works well.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • 1
    that's wrong a bit. even `list.add(1);` will fail because there is nothing to do with large numbers, in this case, it is because of Java will try to auto box int into Integer and we expect Long there. That's why `L` helps. Literal treated as long will be boxed to Long – ikos23 May 05 '18 at 16:15
  • @john true, but how's that relevant? I'm talking about the large numbers to explain the difference between `int` and `long`. I'm not saying that it fails because the numbers are too small or something./ – ItamarG3 May 05 '18 at 16:16
  • And that's correct, if you have something like 1234565756746546 you will get something like 'number is too large' and need to add L to explicitly say it is long literal. but that's not the case with the example provided in the question – ikos23 May 05 '18 at 16:18
  • @john I see. Thanks, I didn't know that. (regarding comment #2:) Yes, I just tested that and you're completely right. Thanks again! – ItamarG3 May 05 '18 at 16:18
  • 1
    yeah, it is better now :) – ikos23 May 05 '18 at 16:27
2

First, this is your code but with all the fixes!

    private static List<Long> list;

    public static void main(String[] args){

        list=new ArrayList<Long>();
        list.add(121145788L);
        list.add(1245898L);

        Long[] arr = new Long[list.size()];

        list.toArray(arr);

    }

And here is some explanation:

  • you have a list of type Long. When you add() items to the list - compiler expects them to be of type Long, but you are adding literal values (plain numbers like 121145788) so those are treated as integers (type int) and are autoboxed to Integer instead of Long. So add L to treat literals as long and then they are autoboxed into Long. Fine for the compiler :)
  • list.toArray(arr); this is the correct method to kinda convert a list to an array. Because the method you used returns array of Object and arr you created is not an array of Object.
  • And again this correct method I used takes T[] as input, so you need to change your arr declaration a bit to make it of type Long instead of an array of primitive type long.

If for some reason you need long[], the easiest way would be:

long[] arr = new long[list.size()];

for (int i=0; i < list.size(); i++) {
    arr[i] = list.get(i);
}
ikos23
  • 4,879
  • 10
  • 41
  • 60
1

Error(16,13): cannot find method add(int), this is because you are passing int instead of a Long value in a list of Long. You should be doing:

list.add(121145788l);

And for the second error follow this.Hope,it helps!

Suvansh
  • 266
  • 1
  • 6
0

JVM doesn't automatically cast int (short, byte, char) to Long. It's explained on the next link.

Java: Why can't I cast int to Long

Converting List<Long> to long[] is possible and also is answered on SO.

How to convert List<Integer> to int[] in Java?

That's the answers:

using Java 8 streams

    long[] arr = list.stream().mapToLong(Long::longValue).toArray();

using Guava

    long[] arr = Longs.toArray(list);

using Apache Commons Lang

    long[] arr = ArrayUtils.toPrimitive(list.toArray(new Long[0]))