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 / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Newbie question (array)

Thread view: 
Reinert Korsnes - 09 Dec 2005 09:52 GMT
Newbie Java question (my main progamming experience is Ada95):

How do I make a new array (A) of length N and at the same time initiate it ?

I can do it this way:
------------------------------------------------
A = new int [N);
for (int i=0; i < A.length; i++){
    A[i] = 1;
}
------------------------------------------------

But I find it clumsy.

In Ada95 one can use:

A : array (0..N) of Integer := (others=>1);

Something I have been missing ?
Possible to do the java version short, elegant and readable/relyable ?

reinert
Thomas Fritsch - 09 Dec 2005 10:08 GMT
> How do I make a new array (A) of length N and at the same time initiate it ?
>
[quoted text clipped - 7 lines]
>
> But I find it clumsy.
You can use java.util.Arrays:
  A = new int[N];
  Arrays.fill(A, 1);

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Mark Thomas - 09 Dec 2005 10:58 GMT
>> How do I make a new array (A) of length N and at the same time
>> initiate it ?
[quoted text clipped - 12 lines]
>   A = new int[N];
>   Arrays.fill(A, 1);

It's also worth noting that the usual Java naming convention is that
only class names start with a capital letter, so variable names should
always start with a lower-case letter.

Mark
Rhino - 09 Dec 2005 15:13 GMT
> Newbie Java question (my main progamming experience is Ada95):
>
> How do I make a new array (A) of length N and at the same time initiate it
> ?

I think you mean "intialize it", not "initiate it". :-)

Sorry, I don't mean to criticize your English, which is surely far better
than my Norwegian. I just want to make sure you know the proper term so that
you won't confuse others with what you mean.

[snip]

> Possible to do the java version short, elegant and readable/relyable ?

'relyable' should be spelled 'reliable'. (Yes, English is full of strange
rules! The verb 'rely' needs to have it's 'y' changed to an 'i' for words
like 'reliable' or 'reliance'. Don't ask me why :-))

Rhino
Paulus de Boska - 09 Dec 2005 15:56 GMT
This can come in handy sometimes, too :
int[] ar = { 1,3,5,7,11 };

Paul Hamaker
SEMM
http://javalessons.com
Roedy Green - 09 Dec 2005 16:21 GMT
On Fri, 09 Dec 2005 10:52:41 +0100, Reinert Korsnes
<reinert.korsnes@chello.no> wrote, quoted or indirectly quoted someone
who said :

>How do I make a new array (A) of length N and at the same time initiate it ?

see http://mindprod.com/jgloss/gotchas.html#ARRAY
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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



©2008 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.