Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:28:32+00:00 2026-06-13T10:28:32+00:00

Possible Duplicate: How to convert an ArrayList containing Integers to primitive int array? I

  • 0

Possible Duplicate:
How to convert an ArrayList containing Integers to primitive int array?

I tried the code below, but it does not work. Do I have to make a loop copy the ArrayList into an array ?

int[]intArray = (int[]) integerArrayList.toArray();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:28:33+00:00Added an answer on June 13, 2026 at 10:28 am

    If you want primitive type array, you can use Apache Commons ArrayUtils#toPrimitive method to get primitive array from wrapper array: –

    public static int[] toPrimitive(Integer[] array)
    

    You can use it like this: –

    int[] arr = ArrayUtils.toPrimitive((Integer[])integerArrayList.toArray());
    

    Or, you can use the 1-arg version of toArray method, that takes an array and returns the array of that type only. That way, you won’t have to the typecasting.

    List<Integer> myList = new ArrayList<Integer>();
    Integer[] wrapperArr = myList.toArray(new Integer[myList.size()]);
    
    // If you want a `primitive` type array
    int[] arr = ArrayUtils.toPrimitive(wrapperArr);
    

    However, if you are Ok with the wrapper type array (Which certainly will not trouble you), then you don’t need to do that last step.


    Also, you can also create a primitive array, without having to create that intermediate Wrapper type array. For that you would have to write it through loop: –

    int[] arr = new int[myList.size()];
    
    for(int i = 0; i < myList.size(); i++) {
        if (myList.get(i) != null) {
            arr[i] = myList.get(i);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to convert an ArrayList containing Integers to primitive int array? How
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: Convert a long hex string in to int array with sscanf I
Possible Duplicate: Assigning an array to an ArrayList in Java I need to convert
Possible Duplicate: Assigning an array to an ArrayList in Java How can I convert
Possible Duplicate: Convert std::tuple to std::array C++11 Suppose you have: template<class T,int N> struct
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Fast way to convert a Bitmap into a Boolean array in C#?
Possible Duplicate: How can I convert String to Int? public List<int> GetListIntKey(int keys) {
Possible Duplicate: convert string to 2D array using php I have the string like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.