Hi
I think I've worked it out fine now thanks everyone.
It doesn't look like this type of for loop is not allowed to run
inside an Object Constructor:-
for (int x:queue)
queue[x]='~';
I've initialised my array ok now in a seperate method now. Maybe Java
is just trying to protect me from myself!!! But it was really
frustrating not understanding by it would only run the loop once!!!
If anyone has anything to add the this please let me know.
Kind Regards
Matt
Daniel Pitts - 20 Oct 2007 19:09 GMT
On Oct 20, 9:09 am, "MattandPaul...@talktalk.net" <mattymu...@aol.com>
wrote:
> Hi
>
[quoted text clipped - 14 lines]
> Kind Regards
> Matt
for (int x:queue) will set x to every value already stored in queue,
not every index...
What you want is something along the lines of
for (int i = 0; i < queue.length; ++i) {
queue[i] = '-';
}
Alternatively, you probably want to use: java.util.Arrays.fill()
<http://java.sun.com/j2se/1.5.0/docs/api/java/util/
Arrays.html#fill(char[],%20char)>
Another note. While it is "valid" to declare "char queue[]", it is
more appropriate in Java to declare it as
"char[] queue"
the "char queue[]" was kept around for the aid (and eventual
detriment) of C programmers. Don't use it :-)
Lew - 20 Oct 2007 19:23 GMT
"MattandPaul...@talktalk.net" wrote:
>> I think I've worked it out fine now thanks everyone.
>>
>> It doesn't look like this type of for loop is not allowed to run
>> inside an Object [sic] Constructor [sic] :-
Of course it is. Well, not inside Object's constructor, of course, since you
don't get to write that one, but inside your class's constructor certainly.
The problem lay in that you were running a loop that always updated element 0
of the array.

Signature
Lew
MattandPauline@talktalk.net schrieb:
> Hi
>
[quoted text clipped - 56 lines]
> Kind Regards
> Matt
you initialization fails..
for (int x:queue) {
queue[x]='~';
}
will get each char out of your array (which are automatically
initialized to a 0 value)
transforms this 0 char to an int with value 0
so what you do is initializing size times the position zero in your
array with the ~ char
MattandPauline@talktalk.net - 20 Oct 2007 17:51 GMT
Thank you. Is there a way to adjust it so that all the array nodes get
assigned '~'
thanks again
Matt
Christian - 20 Oct 2007 17:54 GMT
MattandPauline@talktalk.net schrieb:
> Thank you. Is there a way to adjust it so that all the array nodes get
> assigned '~'
>
> thanks again
> Matt
either you may loop through the array
for (int i = 0; i < queue.length ; i++) {
queue[i] = '~';
}
or use the functions in the arrays class for initalization..
like
Arrays.fill(queue, '~');
"MattandPauline@talktalk.net" <mattymumps@aol.com> wrote :
> Hi
>
[quoted text clipped - 12 lines]
> Queueclass (int size) {
> queue = new char[size]; //create array in class
Great. Creates a char[] of length size, filled with null chars (Unicode
codepoint 0).
> cursor = 0; // also sets end cursor element to 0
>
[quoted text clipped - 3 lines]
>
> }
This is not quite obvious, but maybe if we expand the for loop, the problem
will become clear:
The for loop you wrote is equivalent to:
for(int i = 0; i < queue.length; i++){
int x = queue[i];
queue[x] = '~';
}
All this does, effectively, is set queue[0] to '~' as many times as there
are elements in the array.
What you probably meant was:
for(int x = 0; x < queue.length; x++){
queue[x] = '~';
}
tam@milkyway.gsfc.nasa.gov - 21 Oct 2007 01:54 GMT
> "MattandPaul...@talktalk.net" <mattymu...@aol.com> wrote :
...
are elements in the array.
> What you probably meant was:
>
> for(int x = 0; x < queue.length; x++){
> queue[x] = '~';
>
> }
It caused me great confusion in learning JavaScript that what
seems to be the equivalent syntax to Java's for (x: array) {...}
var arr = new Array(10);
for (var i in arr) {
...
}
would work as the OP had apparently anticipated, i.e., i would take
the values from 0 to 9 in the loop. Since JavaScript arrays
are really more like hash maps between numbers and values
it makes sense there, but it wasn't what I expected. Perl loops
over are similar to Java with
for my $x(@array) {...}
setting $x to each of the elements of the array.
Are there other languages that do it the way it's implemented
in JavaScript? Perhaps the OP was coming to Java with such a
background.
Regards,
Tom McGlynn
Daniel Pitts - 21 Oct 2007 02:23 GMT
> ...
> are elements in the array.
[quoted text clipped - 31 lines]
> Regards,
> Tom McGlynn
Actually, in JavaScript, it would ALSO set the values of any key on
that array object, that wasn't an index.