Possible to create an array and call object constructors at the sametime?

L

laredotornado

Hi,

I'm using Java 1.6. I was wondering if there is a shorter way to do
this. I initialize my array and then loop through the array,
assigning a newly created element at each step ...

DatePref[] prefs = new DatePref[arraySize];
for (int i=0; i<prefs.length; i++) {
prefs = new DatePref();
...
}

I was wondering if there was a more all-in-one solution for
initializing the array and automatically calling the constructor for
each object in the array. It is fine if we change the rules to use
some type of Collection as opposed to an Object[] .

Thanks, - Dave
 
L

Lew

laredotornado said:
I'm using Java 1.6.  I was wondering if there is a shorter way to do
this.  I initialize my array and then loop through the array,
assigning a newly created element at each step ...

DatePref[] prefs = new DatePref[arraySize];
for (int i=0; i<prefs.length; i++) {
     prefs = new DatePref();
}

I was wondering if there was a more all-in-one solution for
initializing the array and automatically calling the constructor for
each object in the array.  It is fine if we change the rules to use
some type of Collection as opposed to an Object[] .


For the final word on array creation, see the JLS:
<http://java.sun.com/docs/books/jls/third_edition/html/
arrays.html#10.3>
<http://java.sun.com/docs/books/jls/third_edition/html/
arrays.html#11358>
<http://java.sun.com/docs/books/jls/third_edition/html/
expressions.html#46168>

The short answer is no, not really. If the array is small enough, you
can sort of do it with

DatePref [] prefs = { new DatePref(), new DatePref(), new DatePref
(), new DatePref(), };

This will rapidly become unsatisfactory.
 
L

Lew

laredotornado said:
I'm using Java 1.6.  I was wondering if there is a shorter way to do
this.  I initialize my array and then loop through the array,
assigning a newly created element at each step ...
DatePref[] prefs = new DatePref[arraySize];
for (int i=0; i<prefs.length; i++) {
     prefs = new DatePref();
}

I was wondering if there was a more all-in-one solution for
initializing the array and automatically calling the constructor for
each object in the array.  It is fine if we change the rules to use
some type of Collection as opposed to an Object[] .


Lew said:
For the final word on array creation, see the JLS:
<http://java.sun.com/docs/books/jls/third_edition/html/
arrays.html#10.3>
<http://java.sun.com/docs/books/jls/third_edition/html/
arrays.html#11358>
<http://java.sun.com/docs/books/jls/third_edition/html/
expressions.html#46168>

The short answer is no, not really. If the array is small enough, you
can sort of do it with

  DatePref [] prefs = { new DatePref(), new DatePref(), new DatePref
(), new DatePref(), };

This will rapidly become unsatisfactory.

Well, there is one exception to that "no": java.util.Arrays.fil().
For reference types it will only fill each slot with the same
reference; it will not fill each slot with an independent reference.
 
E

Eric Sosman

laredotornado said:
Hi,

I'm using Java 1.6. I was wondering if there is a shorter way to do
this. I initialize my array and then loop through the array,
assigning a newly created element at each step ...

DatePref[] prefs = new DatePref[arraySize];
for (int i=0; i<prefs.length; i++) {
prefs = new DatePref();
...
}

I was wondering if there was a more all-in-one solution for
initializing the array and automatically calling the constructor for
each object in the array.


DatePref[] prefs = {
new DatePref(),
new SubclassOfDatePref(),
new AnotherSubclassOfDatePref(),
new StillAnotherSubclassOfDatePref(),
};

Not an enormously practical construct, and no use at all
if arraySize isn't known at code-writing time. The choice of
initializers is meant to illustrate one problem with the idea;
"Others will occur to your thought."
It is fine if we change the rules to use
some type of Collection as opposed to an Object[] .

I'm not sure what you mean by this.
 
L

laredotornado

laredotornado said:
I'm using Java 1.6.  I was wondering if there is a shorter way to do
this.  I initialize my array and then loop through the array,
assigning a newly created element at each step ...
DatePref[] prefs = new DatePref[arraySize];
for (int i=0; i<prefs.length; i++) {
     prefs = new DatePref();
     ...
}

I was wondering if there was a more all-in-one solution for
initializing the array and automatically calling the constructor for
each object in the array.

        DatePref[] prefs = {
            new DatePref(),
            new SubclassOfDatePref(),
            new AnotherSubclassOfDatePref(),
            new StillAnotherSubclassOfDatePref(),
        };

     Not an enormously practical construct, and no use at all
if arraySize isn't known at code-writing time.  The choice of
initializers is meant to illustrate one problem with the idea;
"Others will occur to your thought."
It is fine if we change the rules to use
some type of Collection as opposed to an Object[] .

     I'm not sure what you mean by this.


In other words, I use

DatePref[] prefs = new DatePref[arraySize];

in my example, but I could have used

ArrayList arrayList = new ArrayList();

so long as I can populate ArrayList with an "arraySize" (value not
known at compile time) number of objects, each with a different
reference without having to use a loop. Hope that makes more sense,
although I'm sensing it is not possible to do what I was asking.

- Dave
 
L

Lew

laredotornado said:
I could have used

ArrayList arrayList = new ArrayList();

Of course, in the real world for a list of, say, 'Foo' items, you
should use

List <Foo> foos = new ArrayList <Foo> ();
or
Collection <Foo> foos = new ArrayList <Foo> ();

rather than a concrete raw typed variable whose name reflects its
implementation.
so long as I can populate ArrayList with an "arraySize" (value not
known at compile time) number of objects, each with a different
reference without having to use a loop.  Hope that makes more sense,
although I'm sensing it is not possible to do what I was asking.

A trip through the JLS and the Javadocs will solidify that sense into
certainty. The question is not very outré and the truth is readily
gleaned from the documentation.
 
J

John B. Matthews

laredotornado said:
I'm using Java 1.6. I was wondering if there is a shorter way to do
this. I initialize my array and then loop through the array,
assigning a newly created element at each step ...

DatePref[] prefs = new DatePref[arraySize];
for (int i=0; i<prefs.length; i++) {
prefs = new DatePref();
...
}

I was wondering if there was a more all-in-one solution for
initializing the array and automatically calling the constructor
for each object in the array. It is fine if we change the rules to
use some type of Collection as opposed to an Object[] .


Shorter code or shorter run-time? If you have enough DatePref
instances to matter, consider the flyweight pattern:

<http://www.javaworld.com/javaworld/jw-07-2003/jw-0725-designpatterns.html>
 
E

Eric Sosman

laredotornado said:
laredotornado said:
Hi,
I'm using Java 1.6. I was wondering if there is a shorter way to do
this. I initialize my array and then loop through the array,
assigning a newly created element at each step ...
DatePref[] prefs = new DatePref[arraySize];
for (int i=0; i<prefs.length; i++) {
prefs = new DatePref();
...
}
I was wondering if there was a more all-in-one solution for
initializing the array and automatically calling the constructor for
each object in the array.

DatePref[] prefs = {
new DatePref(),
new SubclassOfDatePref(),
new AnotherSubclassOfDatePref(),
new StillAnotherSubclassOfDatePref(),
};

Not an enormously practical construct, and no use at all
if arraySize isn't known at code-writing time. The choice of
initializers is meant to illustrate one problem with the idea;
"Others will occur to your thought."
It is fine if we change the rules to use
some type of Collection as opposed to an Object[] .
I'm not sure what you mean by this.


Please don't quote signatures.
In other words, I use

DatePref[] prefs = new DatePref[arraySize];

in my example, but I could have used

ArrayList arrayList = new ArrayList();

so long as I can populate ArrayList with an "arraySize" (value not
known at compile time) number of objects, each with a different
reference without having to use a loop. Hope that makes more sense,
although I'm sensing it is not possible to do what I was asking.

If you could solve the first problem, you could then do

ArrayList<DatePref> = new ArrayList<DatePref>(
Arrays.asList(prefs));

However, I'm doubtful about the usefulness of the shortcut
in the first place. The magically populated array would be
useful if you needed N instances, all created by the no-arguments
constructor and hence all "identical" (unless the constructor is
supplying a serial number or a high-precision time stamp or a
random initial value or some such). Having N "identical" objects
floating around may sometimes be useful, but not often: What good
are N immutable zero-valued Integers, for example? Perhaps you
will go on to "customize" the (mutable) new objects -- but if
you're writing a customization loop, you might as well construct
them at the same time. Usually, anyhow.

Do you have a concrete example of a situation you've actually
encountered where the ability to create an array and populate it
with N identical objects would have come in handy? Last time this
topic came around (six-ish months ago, I think, but I wouldn't
swear to it), lots of contrived "if you ever wanted to X" examples
were offered, but I don't recall seeing any "this actually happened
to me" accounts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top