Possible to instantiate array with auto-instantiated empty objects?

So for example if you wanted to create an ArrayList<T>[], is there any way neater/cheaper to instantiate the array elements as ArrayList<T>() than iterating through the array and instantiating them manually?
[226 byte] By [KomodoDavea] at [2007-11-26 15:39:05]
# 1

I really don't think there is I might be wrong, but I don't think so. The Collection api specifies that the constructor of ArrayList (and it's associated siblings) take a Collection so you could give them another type of list and it would be happy, or anything else from the Collection group but not an array.

Follow the bouncing link.

http://java.sun.com/docs/books/tutorial/collections/index.html

Hope this helps,

PS.

puckstopper31a at 2007-7-8 21:57:24 > top of Java-index,Java Essentials,Java Programming...
# 2
is there any way neater/cheaper to> instantiate the array elements as ArrayList<T>() than> iterating through the array and instantiating them> manually?Not really.
tjacobs01a at 2007-7-8 21:57:24 > top of Java-index,Java Essentials,Java Programming...
# 3
for one thing not every class has a default () constructor
tjacobs01a at 2007-7-8 21:57:24 > top of Java-index,Java Essentials,Java Programming...
# 4
You can use an array to populate the list.ArrayList<String> list = new ArrayList<String>(Arrays.asList("Moe", "Larry", "Curly"));
ChuckBinga at 2007-7-8 21:57:24 > top of Java-index,Java Essentials,Java Programming...
# 5

You could write a fill method like the following, although I threw up in my mouth a little when I wrote this code...

import java.util.*;

public class Test {

public static void main(String[] args) {

List[] a = (List[]) fill(new ArrayList[5]);

for (int i = 0; i<a.length; ++i)

a[i].add("testing");

}

static Object[] fill(Object[] a) {

try {

Class cls = a.getClass().getComponentType();

for(int i = 0; i><a.length; ++i) {

a[i] = cls.newInstance();

}

} catch (Exception e) {

e.printStackTrace();

}

return a;

}

}

>

DrLaszloJamfa at 2007-7-8 21:57:24 > top of Java-index,Java Essentials,Java Programming...
# 6

Thanks for your info and suggestions. It's a shame there's no easy way to do it... it narks me that I have to waste computational time iterating through a 3D array I have right now (for a particle system) just so that the elements are instantiated; I'm forced to do it like that, because the cunningly optimized force algorithms a friend and I have conjured up don't necessarily visit the nodes in an ordered fashion, and it would be more expensive to have a test for null every time I visit a 3D cell. Oh well.

Thank you again everyone.

> You could write a fill method like the following,

> although I threw up in my mouth a little when I wrote

> this code...

LOL! Neat example though, nonetheless :o)

KomodoDavea at 2007-7-8 21:57:24 > top of Java-index,Java Essentials,Java Programming...
# 7
> it narks me that I> have to waste computational time iterating through a> 3D array I have right now (for a particle system)Sounds like a pretty neat problem.%
duffymoa at 2007-7-8 21:57:24 > top of Java-index,Java Essentials,Java Programming...