Using ArrayList.toArray()

Hi,

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

Thanks
 
DJ_AM_JuiceboxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
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;
	    System.out.println(test[0].color);
	    
	    test2[0].color = 3;
	    System.out.println(test2[0].color);
	    
	    cars.get(0).color = 7;
	    
	    System.out.println(test[0].color);
	    System.out.println(test2[0].color);
	    System.out.println(cars.get(0).color);
	}

Open in new window

0
Kevin CrossChief Technology OfficerCommented:
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!
0
sciuriwareCommented:
But, this works also:

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

;JOOP!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.