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 7594027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:18:50+00:00 2026-05-30T21:18:50+00:00

How can I convert a List to an Array in Java? Check the code

  • 0

How can I convert a List to an Array in Java?

Check the code below:

ArrayList<Tienda> tiendas;
List<Tienda> tiendasList; 
tiendas = new ArrayList<Tienda>();

Resources res = this.getBaseContext().getResources();
XMLParser saxparser =  new XMLParser(marca,res);

tiendasList = saxparser.parse(marca,res);
tiendas = tiendasList.toArray();

this.adaptador = new adaptadorMarca(this, R.layout.filamarca, tiendas);
setListAdapter(this.adaptador);  

I need to populate the array tiendas with the values of tiendasList.

  • 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-05-30T21:18:50+00:00Added an answer on May 30, 2026 at 9:18 pm

    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 reference types. For arrays of primitive types, use the traditional way:

    List<Integer> list = ...;
    int[] array = new int[list.size()];
    for(int i = 0; i < list.size(); i++) array[i] = list.get(i);
    

    Update:

    It is recommended now to use list.toArray(new Foo[0]);, not list.toArray(new Foo[list.size()]);.

    From JetBrains Intellij Idea inspection:

    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[0]).

    In
    older Java versions using pre-sized array was recommended, as the
    reflection call which is necessary to create an array of proper size
    was quite slow. However since late updates of OpenJDK 6 this call
    was intrinsified, making the performance of the empty array version
    the same and sometimes even better, compared to the pre-sized
    version. Also passing pre-sized array is dangerous for a concurrent or
    synchronized collection as a data race is possible between the
    size and toArray call which may result in extra nulls
    at the end of the array, if the collection was concurrently shrunk
    during the operation.

    This inspection allows to follow the
    uniform style: either using an empty array (which is recommended in
    modern Java) or using a pre-sized array (which might be faster in
    older Java versions or non-HotSpot based JVMs).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the sample code below, i need to convert the list nvlist back to
In Java how do you convert a ArrayList into a two dimensional array Object[][]?
I want to convert IList to array: Please see my code: IList list =
How can I convert a List<MyObject> to an IEnumerable<MyObject> and then back again? I
How can I convert a list of strings (each of which represents a number,
How can I convert a Char[] (of any length) to a List ?
I'm trying to convert the following list into a select list so it can
How i can convert to c# code files to html files?Is there any generation
Seeing as you can convert any document to a byte array and save it
In Java I want to convert a nested List which contains at the deepest

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.