Using ArrayList.toArray()

Posted on 2008-11-06
Last Modified: 2008-11-07

I'm using java 1.5, how do we use toArray? I have something like:

    public class Car
        int color;
    ArrayList<Car> cars  = new ArrayList<Car>();
    cars.add(new Car());

    Car[] test = cars.toArray();

Yeah I just need a plain array representation of the array list.

Will the produced array be a reference to the original ArrayList, or is it a completely new object (so changes to one will not affect the other?)

Question by:DJ_AM_Juicebox
    LVL 86

    Accepted Solution

    You can do the following. It's a new object in its own right
    Car[] test = (Car[])cars.toArray(new Car[cars.size()]);

    Open in new window

    LVL 59

    Expert Comment

    by:Kevin Cross
    Java is pass by value, but for objects what you are passing by value is the reference to Object itself so change in a Car from array will change value in Car of List, etc.

    As a personal preference, I usually use the List interface to define variable and instantiate ArrayList as the implementation, so my code has that difference but works same with how you have it.

    Here is an example code to illustrate what I mean with values.
    public static void main(String[] args) {
    		List<Car> cars  = new ArrayList<Car>();
    	    cars.add(new Car());
    	    Car[] test = cars.toArray(new Car[cars.size()]);
    	    Car[] test2 = cars.toArray(test);
    	    test[0].color = 1;
    	    test2[0].color = 3;
    	    cars.get(0).color = 7;

    Open in new window

    LVL 59

    Expert Comment

    by:Kevin Cross
    And I see CEHJ already pointed out how to get the array...

    FYI, since using the toArray(T[]) method, it already returns an object of type T[] which in this case is Car[]; therefore, the extra cast to (Car[]) is not required.
    Car[] test = cars.toArray(new Car[cars.size()]);

    Good luck!
    LVL 24

    Expert Comment

    But, this works also:

                                      Car[] test = cars.toArray(new Car[0]);


    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    instanceof  operator in java 26 56
    Logs to delete in Windows VCenter server ? 11 92
    Unable to start eclipse ? 17 48
    @SBGen Method 3 16
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now