Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Array of Objects - Qsn

Thread view: 
alij - 25 May 2007 03:49 GMT
Hi,

I was wondering if there is a way to call a specific constructor when
creating an array in Java. I have an object whose constructor takes in
some parameters, and I want to set up an array of that Object with a
set of Parameters.

Thanks,

Alij
Knute Johnson - 25 May 2007 04:33 GMT
> Hi,
>
[quoted text clipped - 6 lines]
>
> Alij

YourObject[] obs = new YourObject[??];
for (int i=0; i<obs.length; i++)
    obs = new YourObject(param1,param2,param3);

Signature

Knute Johnson
email s/nospam/knute/

Mike Schilling - 25 May 2007 08:41 GMT
> Hi,
>
> I was wondering if there is a way to call a specific constructor when
> creating an array in Java. I have an object whose constructor takes in
> some parameters, and I want to set up an array of that Object with a
> set of Parameters.

No.  You'll have to write a loop.
blmblm@myrealbox.com - 25 May 2007 12:19 GMT
> > Hi,
> >
[quoted text clipped - 4 lines]
>
> No.  You'll have to write a loop.

Which the OP would need to do in any case, right?  or is
there some neat trick I don't know for creating an array of
objects without an explicit loop?

I mention this because the OP's question makes it sound like
he/she [*] thinks that maybe "new MyObject[10]" creates
an array of 10 MyObjects (presumably using the default
constructor), when actually all it creates is 10 references.

[*] Sorry, but which pronoun goes with "alij" isn't obvious
to this US person ....

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

Lew - 25 May 2007 12:36 GMT
> he/she [*]
>
> [*] Sorry, but which pronoun goes with "alij" isn't obvious
> to this US person ....

Use "they" - "they had a question about arrays".  This is well-established
English usage going back to well before Shakespeare.

Signature

Lew

blmblm@myrealbox.com - 25 May 2007 13:43 GMT
> > he/she [*]
> >
[quoted text clipped - 3 lines]
> Use "they" - "they had a question about arrays".  This is well-established
> English usage going back to well before Shakespeare.

It's a sensible solution, I suppose, but I'm too much of an,
um, prescriptivist? [*] when it comes to language usage to
want to adopt it.  "YMMV", maybe.

[*] With some exceptions, such as the placement of commas and
periods relative to quotation marks.

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

Lew - 25 May 2007 13:56 GMT
>>> he/she [*]
>>>
[quoted text clipped - 9 lines]
> [*] With some exceptions, such as the placement of commas and
> periods relative to quotation marks.

But it doesn't violate prescriptive grammar.  It has a long and venerable
history.  Its use probably predates the "rule" you think you have to follow.

Signature

Lew

Oliver Wong - 25 May 2007 17:00 GMT
>>>> he/she [*]
>>>> [*] Sorry, but which pronoun goes with "alij" isn't obvious
[quoted text clipped - 12 lines]
> venerable history.  Its use probably predates the "rule" you think you
> have to follow.

   Depends on which grammar you believe in. There exists English grammars
which favour "he or she" over "they" for a singular referent of unknown
gender, and some of those grammar are probably pretty "old" too (no idea
if they are mostly older than the grammars which favour "they" or not).

   I'll switch back and forth between "he or she" and "they" depending on
my mood.

   - Oliver
blmblm@myrealbox.com - 25 May 2007 17:05 GMT
> >>> he/she [*]
> >>>
[quoted text clipped - 12 lines]
> But it doesn't violate prescriptive grammar.  It has a long and venerable
> history.  Its use probably predates the "rule" you think you have to follow.

For the record, I guess .... :

Look, I'm aware that use of "they" as a singular pronoun
is accepted usage in some circles and that there is a case
to be made for it.  (I also hang out in alt.usage.english.)
To me it feels wrong, however, and I doubt that any amount
of reasoned argument is going to change that.  I say let's
agree to differ.

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

Lew - 25 May 2007 18:21 GMT
Cross-posted from comp.lang.java.programmer, on the subject of using "they" as
a gender-neutral personal singular pronoun:

> Look, I'm aware that use of "they" as a singular pronoun
> is accepted usage in some circles and that there is a case
> to be made for it.  (I also hang out in alt.usage.english.)
> To me it feels wrong, however, and I doubt that any amount
> of reasoned argument is going to change that.  I say let's
> agree to differ.

I'll go past that.  I'll agree that "they" as a singular neutral will always
sound less formal than "he or she", similarly to the equally-venerable and
only relatively recently-denigrated "ain't".  In other words, you are correct
in your reticence.

I prefer the elegance of "they" to the awkwardness of "he or she", whose very
formality is what dissuades me from using it.

The abortive attempts to create a brand-new politically-correct pronoun ("te",
"ter") are just plain silly.

I am cross-posting to alt.usage.english and setting f/u there to keep things
clear here in clj.programmer.

Signature

Lew

Eric Sosman - 25 May 2007 16:43 GMT
blmblm@myrealbox.com wrote On 05/25/07 07:19,:

>>>Hi,
>>>
[quoted text clipped - 8 lines]
> there some neat trick I don't know for creating an array of
> objects without an explicit loop?

    Thing[] thing = new Thing[] {
       new Thing(42),
       new Thing("abc"),
       new SubclassOfThing(new Date()),
    };

Signature

Eric.Sosman@sun.com

blmblm@myrealbox.com - 25 May 2007 17:11 GMT
> blmblm@myrealbox.com wrote On 05/25/07 07:19,:
> >
[quoted text clipped - 16 lines]
>        new SubclassOfThing(new Date()),
>     };

Well, you got me on that one!  But I can't think how to state the
question I actually had in mind more precisely, so -- oh well.

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

Mike Schilling - 25 May 2007 17:27 GMT
>> blmblm@myrealbox.com wrote On 05/25/07 07:19,:
>> >
[quoted text clipped - 19 lines]
> Well, you got me on that one!  But I can't think how to state the
> question I actually had in mind more precisely, so -- oh well.

I think you're saying that there's no way to create a array of objects whose
size is determined at run-time without a loop.
Oliver Wong - 25 May 2007 18:30 GMT
>>> blmblm@myrealbox.com wrote On 05/25/07 07:19,:
>>> > is
[quoted text clipped - 12 lines]
> I think you're saying that there's no way to create a array of objects
> whose size is determined at run-time without a loop.

public class Test4 {
 public static Object[]
makeArrayOfObjectsWhoseSizeIsDeterminedAtRuntimeWithoutALoop(int size) {
   if (size < 0) {
     throw new IllegalArgumentException();
   }
   if (size == 0) {
     return new Object[0];
   }
   Object[] returnValue = new Object[size];
   returnValue[0] = new Object();
   System.arraycopy(makeArrayOfObjectsWhoseSizeIsDeterminedAtRuntimeWithoutALoop(size
- 1), 0, returnValue, 1, size - 1);
   return returnValue;
 }
}

   Or, since array indexes must be java integer, and there's a finite
number of them:

public class Test4 {
 public static Object[]
makeArrayOfObjectsWhoseSizeIsDeterminedAtRuntimeWithoutALoop(
     int size) {
   switch (size) {
   case 0:
     return new Object[] {};
   case 1:
     return new Object[] { new Object() };
   case 2:
     return new Object[] { new Object(), new Object() };

   /*etc.*/

   default:
     throw new IllegalArgumentException();
   }
 }
}

   - Oliver
Daniel Pitts - 25 May 2007 19:04 GMT
> > <blm...@myrealbox.com> wrote in message
> >news:5bog4pF2u1ladU2@mid.individual.net...
[quoted text clipped - 58 lines]
>
>     - Oliver

Collections.nCopies(count, new Object()).toArray();
blmblm@myrealbox.com - 26 May 2007 17:40 GMT
> > > <blm...@myrealbox.com> wrote in message
> > >news:5bog4pF2u1ladU2@mid.individual.net...
[quoted text clipped - 14 lines]
> > > I think you're saying that there's no way to create a array of objects
> > > whose size is determined at run-time without a loop.

Yes, thanks! --  well, except that I'm not saying there isn't a way,
but asking whether there is one, and indeed there appear to be
several, if you're clever and/or know the Collections class better
than I do:

> > public class Test4 {
> >   public static Object[]
[quoted text clipped - 42 lines]
>
> Collections.nCopies(count, new Object()).toArray();

Signature

B. L. Massingill
ObDisclaimer:  I don't speak for my employers; they return the favor.

voorth - 25 May 2007 09:13 GMT
> Hi,
>
> I was wondering if there is a way to call a specific constructor when
> creating an array in Java. I have an object whose constructor takes in
> some parameters, and I want to set up an array of that Object with a
> set of Parameters.

Something like this:

 public void createArray() {
   String[] array = {new String("Hello"), new String("World")};
 }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.