Setting up object arrays - loops not loopng enough times

M

MattandPauline

Hi

I'm new to Java programming and seem to have got stuck.

Please can anyone say what is wrong with my basic program below. I'm
trying to create an object array that holds tilda (~) characters :-

class Queueclass {

char queue[]; // name of queue
int cursor; // position at end of queue for adding extra nodes into
queue

//------------------Define Constructor------------------------
Queueclass (int size) {
queue = new char[size]; //create array in class
cursor = 0; // also sets end cursor element to 0

// initialising array
for (int x:queue)
queue[x]='~';

}
//-----------------End of Defining Constructor-------------------

//-----------------Define Methods--------------------------------
char display(int node){
return queue[node];
}



}

public class Queuecirc2 {

public static void main(String[] args) {
Queueclass queue1= new Queueclass(10); //define new object called
queue1

char ch; // character returned

// Checking default that defailt values of queue1 are set to
tilda's (~)
int x = 0;
do {
ch=queue1.display(x);
System.out.println("Character returned is :-" + ch);
x=x+1;
} while(x < 9);
}
}

Its as though my program is not following the do and for loops
properly. The output is :-
Character returned is :-~
Character returned is :-

Any help is much appreciated.

Kind Regards
Matt
 
M

MattandPauline

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
 
C

Christian

Hi

I'm new to Java programming and seem to have got stuck.

Please can anyone say what is wrong with my basic program below. I'm
trying to create an object array that holds tilda (~) characters :-

class Queueclass {

char queue[]; // name of queue
int cursor; // position at end of queue for adding extra nodes into
queue

//------------------Define Constructor------------------------
Queueclass (int size) {
queue = new char[size]; //create array in class
cursor = 0; // also sets end cursor element to 0

// initialising array
for (int x:queue)
queue[x]='~';

}
//-----------------End of Defining Constructor-------------------

//-----------------Define Methods--------------------------------
char display(int node){
return queue[node];
}



}

public class Queuecirc2 {

public static void main(String[] args) {
Queueclass queue1= new Queueclass(10); //define new object called
queue1

char ch; // character returned

// Checking default that defailt values of queue1 are set to
tilda's (~)
int x = 0;
do {
ch=queue1.display(x);
System.out.println("Character returned is :-" + ch);
x=x+1;
} while(x < 9);
}
}

Its as though my program is not following the do and for loops
properly. The output is :-
Character returned is :-~
Character returned is :-

Any help is much appreciated.

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
 
M

MattandPauline

Thank you. Is there a way to adjust it so that all the array nodes get
assigned '~'

thanks again
Matt
 
C

Christian

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 = '~';
}

or use the functions in the arrays class for initalization..
like

Arrays.fill(queue, '~');
 
D

Daniel Pitts

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

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 = '-';
}

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 :)
 
L

Lew

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.
 
A

Adam Maass

Hi

I'm new to Java programming and seem to have got stuck.

Please can anyone say what is wrong with my basic program below. I'm
trying to create an object array that holds tilda (~) characters :-

class Queueclass {

char queue[]; // name of queue
int cursor; // position at end of queue for adding extra nodes into
queue

//------------------Define Constructor------------------------
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

// initialising array
for (int x:queue)
queue[x]='~';

}


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;
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] = '~';
}
 
T

tam

....
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
 
D

Daniel Pitts

On Oct 20, 8:43 pm, "Adam Maass" <[email protected]>

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

Actually, in JavaScript, it would ALSO set the values of any key on
that array object, that wasn't an index.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top