File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Stupid question about initializing arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stupid question about initializing arrays" Watch "Stupid question about initializing arrays" New topic
Author

Stupid question about initializing arrays

Craig Flannagan
Greenhorn

Joined: Oct 21, 2001
Posts: 10
What's the most slickest way to initialize arrays (which are attributes) in a constructor?
For example, the following code does not work:

So I have to do the following instead:

Two questions:
Was the 2nd code snipplet the best "professional" way to do it?
And why wouldn't the first code snipplet work, even though I can do it if I declare an array and initialize all on a single line?
Many thanks!
CF
[ April 25, 2002: Message edited by: Craig Flannagan ]

<blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre><font size="2"><br />boolean isDevilish(Human h)<br />{<br /> return (h==CraigFlannagan);<br />}<br /></font></pre><hr></blockquote>
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
Hi.
well i woudlnt do it the second way.. just doesnt sem that professional tome but i may be wrong.
If you really want to intialize it why not like you said just do it at the first line when you define th e variable too.
like:
int[] numbers={4,5,6,7}
what diffrence does it make if you put it in the constructor or not?
the intialization will take place anyway every time you create an object.
thats how i would do it anyway, but im sure a bartendar here will answer a better reply.
Craig Flannagan
Greenhorn

Joined: Oct 21, 2001
Posts: 10
Ok, bad example.
What about this scenario:
Declaring array as an attribute (no initialization)...
And then initialize the array in the constructor with values passed as arguements...
How would you handle this?
Thanks,
CF
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
hmm.
i see your point.
in that case i would do exactly as you did.
maybe just as a precaution you could also set the temp array at the end to null just to make sure you dont have 2 refercnes to it. although in this case because its a local variable it will get deleted anyway.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Try this:


"I'm not back." - Bill Harding, Twister
Craig Flannagan
Greenhorn

Joined: Oct 21, 2001
Posts: 10
Jim Yingst -
That's the first time I've seen anything of that sort. Thanks for the education!
-CF
[ April 25, 2002: Message edited by: Craig Flannagan ]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
wow!
same here.
thanks Jim.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stupid question about initializing arrays
 
Similar Threads
this problem baffles me
How to combine two arrays within a loop?
Making an array with random numbers
I am a real JAVA beginner, please take a look at this code.
Nested for loop for grid