Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a List to an Array in Java

Tags:

java

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.

like image 531
redsoxlost Avatar asked Apr 09 '26 02:04

redsoxlost


2 Answers

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.

like image 155
ItamarG3 Avatar answered Apr 10 '26 16:04

ItamarG3


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);
}
like image 22
ikos23 Avatar answered Apr 10 '26 15:04

ikos23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!