0

I have created an ArrayList of Integer wrapper class, then converted it into Integer type array. Now my goal is to convert it into int type array. Try to solve the problem using ArrayUtils.toPrimitive() method. But it's having some issues. Here is my code:

import java.util.ArrayList;
import java.util.Arrays;
import java.lang.Object;

public class ArrayListToArray {
    public static void main(String[] args) {

        // Convert to int array in one line

        ArrayList<Integer> list = new ArrayList<Integer>();

        list.add(Integer.parseInt("1"));
        list.add(Integer.parseInt("2"));
        list.add(Integer.parseInt("3"));
        list.add(Integer.parseInt("4"));

        Integer[] wrapperArr = list.toArray(new Integer[list.size()]);

        // If you want a `primitive` type array
        int[] arr = ArrayUtils.toPrimitive(wrapperArr);

        System.out.println(Arrays.toString(arr));
    }
}

It's showing error ArrayUtils can't be resolved. And now executing the code.

How to solve the problem without writing any loop ? I am exploring java built-in classes, methods and packages.

Or is there any other methods other than ArrayUtils, to do the same ?

Thanks in advance. :-)

3
  • 1
    ArrayUtils seems to come from Apache Commons. For a solution without dependencies try this: int[] arr = list.stream().mapToInt(Integer::valueOf).toArray(); Commented May 21, 2020 at 6:37
  • ^ You should prefer that, but you can also import ArrayUtils with: import org.apache.commons.lang3.ArrayUtils;. Commented May 21, 2020 at 6:38
  • Thanks @JohannesKuhn, your tip has worked for me. Commented May 21, 2020 at 7:13

3 Answers 3

1

Solution for Integer to int array conversion, without ArrayUtils:

    final int[] arr = new int[wrapperArr.length];
    Arrays.setAll(arr, i -> wrapperArr[i]);

And vice versa, int to Integer array:

    final Integer[] wrapperArr = new Integer[arr.length];
    Arrays.setAll(wrapperArr, i -> arr[i]);

Bonus:

  1. ArrayList of Integers to int array.

     final List<Integer> list = new ArrayList<>();
     list.add(1);
     list.add(2);
     list.add(3);
    
     final int[] arr = new int[list.size()];
     Arrays.setAll(arr, list::get);
    
  2. ArrayList of Integers (as Strings) to int array.

     final List<String> list = new ArrayList<>();
     list.add("1");
     list.add("2");
     list.add("3");
    
     final int[] arr = new int[list.size()];
     Arrays.setAll(arr, i -> Integer.parseInt(list.get(i)));
    
Sign up to request clarification or add additional context in comments.

Comments

1

Your import may be wrong or the "commons-lang3" jar may not be in the build path.

import org.apache.commons.lang3.ArrayUtils

3 Comments

I tried that too. But it shows The import org.apache cannot be resolvedJava(268435846)
Check your class path and build path whether the "commons-lang3" jar available or not
Or use Maven or Gradle.
1

One possibility is that, your code refers to an ArrayUtils class that is not provided by the org.apache.commons.commons-lang3 but from some other dependency. Re-check your imports.

Delete your current import and re-import. IDE might help suggesting the classes. Make sure to use the org.apache.commons.lang3.ArrayUtils import . Make sure you have the below dependency in the pom.xml.

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.9</version>
    </dependency>

However, unless there is a specific reason, use the java streams() and get the primitive array instead of using an external dependency

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.