[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using ArrayList.toArray()

Posted on 2008-11-06
4
Medium Priority
?
853 Views
Last Modified: 2008-11-07
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
 
0
Comment
Question by:DJ_AM_Juicebox
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 22897001
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22897063
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22897139
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 22903375
But, this works also:

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

;JOOP!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 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