Converting a List to an Array in Java

java list to array int
list.toarray java
convert list to array python
convert list to array c#
arraylist to array java int
convert list of list to array java
convert list to arraylist java
arraylist to array java 8

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.

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.

Convert List to Array in Java, In this post, we will see how to convert list to array in Java. 1. List.toArray() 2. Java 8 Streams API 3. Guava's FluentIterable & Iterable class Using Arrays.asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class. Collections.addAll() method - Create a new list before using this method and then add array elements using this method to existing list. Iteration method - Create a new list. Iterate the array and add each element to the list.

Convert List to Array in Java, Java program to convert a List to an array. // using get() in a loop. import java.io.*;. import java.util.List;. import java.util.LinkedList;. class GFG. {. public static void  If the passed array doesn’t have enough space, a new array is created with same type and size of given list. If the passed array has more space, the array is first filled with list elements, then null values are filled.

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!

List to array in Java, Either: Foo[] array = list.toArray(new Foo[0]);. or: Foo[] array = new Foo[list.size()]; list.toArray(array); // fill the array. Note that this works only for arrays of  There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String).

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]))

Convert list to array in Java, There is no easy way to convert an array to list in Java, but you can easily convert a list into array by calling toArray() method, which List inherit from Collection  Converting Array to List. To convert an array to a List, you can use the Arrays.asList() method. This method returns a fixed-size list from the elements of the array passed to it. An ArrayToListConverter class that converts an array to a List is this. ArrayToListConverter.java

Convert List to Array in Java, import java.util.ArrayList; public class ListToArray { public static void main(String[] args) { // A list of size 4 which is to be converted: ArrayList<Integer> list = new  2. Java 8. In Java 8, we can use Stream API to convert list to array. The idea is to convert given list to Stream using List.stream() function and use Stream.toArray() method to return an array containing the elements of the stream.

How to convert list to array in Java and vice-versa, Converting between List and Array is a very common operation in Java. The best and easiest way to convert a List into an Array in Java is to  Syntax: public static List asList(T a) // Returns a fixed-size List as of size of given array. // Element Type of List is of same as type of array element type. // It returns an List containing all of the elements in this // array in the same order. // T represents generics. Note that the there is an array parameter and List return value.

How to convert a Java list to an array, How to Convert between an Array and a List Using plain Java, Guava or Apache Commons Collections. Learn to convert ArrayList to array using toArray() method with example.toArray() method returns an array containing all of the elements in the list in proper sequence (from first to last element).

Comments
  • what java version are you using?
  • 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.
  • 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 ....
  • 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
  • @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./
  • 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
  • @john I see. Thanks, I didn't know that. (regarding comment #2:) Yes, I just tested that and you're completely right. Thanks again!
  • yeah, it is better now :)
  • So there is no way I cant use an array of type long?
  • @redsoxlost I added it to my answer