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 / General / October 2007

Tip: Looking for answers? Try searching our database.

Setting up object arrays - loops not loopng enough times

Thread view: 
MattandPauline@talktalk.net - 20 Oct 2007 16:45 GMT
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
MattandPauline@talktalk.net - 20 Oct 2007 17:09 GMT
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

Christian - 20 Oct 2007 17:15 GMT
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, '~');
Adam Maass - 21 Oct 2007 01:43 GMT
"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.


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.