简体   繁体   中英

Converting a wrapper class array to primitive array using ArrayUtils

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. :-)

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

import org.apache.commons.lang3.ArrayUtils

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM