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.