list of loop objects...

Discussion in 'Java' started by mai, Nov 8, 2006.

  1. mai

    mai Guest

    Hi,,
    I'm learning Java,, and I was trying to creat a function that performs
    a three-level nested loop and returns a list of loop objects indicating
    progress through the loop,,but when I ran this code:

    package loop;
    import java.util.ArrayList;
    import java.util.List;

    public class Loop{
    public int level;
    public int counter1;
    public int counter2;
    public int counter3;

    public Loop(int level,int c1, int c2, int c3){
    this.level=level;
    counter1 = c1;
    counter2 = c2;
    counter3 = c3;
    }
    public static List<Loop> doLoops() {
    List<Loop> result = new ArrayList<Loop>();
    int cnt1=0;
    int cnt2=0;
    int cnt3=0;

    outer: for(;;){
    Loop ob1;
    ob1= new Loop(1, cnt1, cnt2, cnt3);
    result.add(ob1);
    cnt1 +=1;


    middle: for(;;){
    Loop ob2;
    ob2= new Loop(2,cnt1,cnt2,cnt3);
    result.add(ob2);
    cnt2+= 1;

    inner : for(;;){
    Loop ob3;
    ob3 = new Loop(3,cnt1,cnt2,cnt3);
    result.add(ob3);
    cnt3 +=1;

    if (cnt3 % 5 == 0)
    break inner;

    if (cnt3 % 11 == 0)
    break middle;

    if (cnt3 % 14 == 0)
    break outer;

    }
    }
    }
    return result;
    }

    public static void main(String[] argv) {
    int i;
    for(i=0; i< doLoops().size(); i++)

    System.out.println((doLoops().get(i)));

    }
    }
    I got the following results,,

    [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]

    Please help me,,
     
    mai, Nov 8, 2006
    #1
    1. Advertising

  2. mai

    Oliver Wong Guest

    "mai" <> wrote in message
    news:...
    > Hi,,
    > I'm learning Java,, and I was trying to creat a function that performs
    > a three-level nested loop and returns a list of loop objects indicating
    > progress through the loop,,but when I ran this code:


    [code snipped]

    > I got the following results,,
    >
    > [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    > loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    > loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    > loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    > loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    > loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]


    What results did you expect (this isn't a sarcastic or rhetorical
    question -- I really don't know what it is you're trying to do)? Perhaps you
    meant to override the toString() method? See
    http://www.javapractices.com/Topic55.cjp

    - Oliver
     
    Oliver Wong, Nov 8, 2006
    #2
    1. Advertising

  3. mai wrote:
    > Hi,,
    > I'm learning Java,, and I was trying to creat a function that performs
    > a three-level nested loop and returns a list of loop objects indicating
    > progress through the loop,,but when I ran this code:
    >
    > package loop;
    > import java.util.ArrayList;
    > import java.util.List;
    >
    > public class Loop{
    > public int level;
    > public int counter1;
    > public int counter2;
    > public int counter3;
    >
    > public Loop(int level,int c1, int c2, int c3){
    > this.level=level;
    > counter1 = c1;
    > counter2 = c2;
    > counter3 = c3;
    > }
    > public static List<Loop> doLoops() {


    <snip>

    > public static void main(String[] argv) {
    > int i;
    > for(i=0; i< doLoops().size(); i++)
    >
    > System.out.println((doLoops().get(i)));


    That returns a reference to the i'th Loop object in the List returned by
    doLoops(). If you print a reference you'll get the sort of output you
    show below. Maybe you should write a toString() method for your Loop
    object and then

    System.out.println( doLoops().get(i).toString() );

    > I got the following results,,
    >
    > [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    > loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    > loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    > loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    > loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    > loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]
     
    RedGrittyBrick, Nov 8, 2006
    #3
  4. mai

    mai Guest

    I was trying to print the values of the list. Which are ,supposesdly,
    integer values,
    Oliver Wong wrote:
    > "mai" <> wrote in message
    > news:...
    > > Hi,,
    > > I'm learning Java,, and I was trying to creat a function that performs
    > > a three-level nested loop and returns a list of loop objects indicating
    > > progress through the loop,,but when I ran this code:

    >
    > [code snipped]
    >
    > > I got the following results,,
    > >
    > > [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    > > loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    > > loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    > > loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    > > loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    > > loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]

    >
    > What results did you expect (this isn't a sarcastic or rhetorical
    > question -- I really don't know what it is you're trying to do)? Perhaps you
    > meant to override the toString() method? See
    > http://www.javapractices.com/Topic55.cjp
    >
    > - Oliver
     
    mai, Nov 8, 2006
    #4
  5. mai

    mai Guest

    Hi,, Thanks for your help,, but I still get the same results even after
    writing:
    System.out.println(doLoops().get(i).toString());

    Please help,,
    mai wrote:
    > I was trying to print the values of the list. Which are ,supposesdly,
    > integer values,
    > Oliver Wong wrote:
    > > "mai" <> wrote in message
    > > news:...
    > > > Hi,,
    > > > I'm learning Java,, and I was trying to creat a function that performs
    > > > a three-level nested loop and returns a list of loop objects indicating
    > > > progress through the loop,,but when I ran this code:

    > >
    > > [code snipped]
    > >
    > > > I got the following results,,
    > > >
    > > > [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    > > > loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    > > > loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    > > > loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    > > > loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    > > > loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]

    > >
    > > What results did you expect (this isn't a sarcastic or rhetorical
    > > question -- I really don't know what it is you're trying to do)? Perhaps you
    > > meant to override the toString() method? See
    > > http://www.javapractices.com/Topic55.cjp
    > >
    > > - Oliver
     
    mai, Nov 8, 2006
    #5
  6. mai wrote:
    > Hi,, Thanks for your help,, but I still get the same results even after
    > writing:
    > System.out.println(doLoops().get(i).toString());
    >
    > Please help,,
    > mai wrote:
    >> I was trying to print the values of the list. Which are ,supposesdly,
    >> integer values,
    >> Oliver Wong wrote:
    >>> "mai" <> wrote in message
    >>> news:...
    >>>> Hi,,
    >>>> I'm learning Java,, and I was trying to creat a function that performs
    >>>> a three-level nested loop and returns a list of loop objects indicating
    >>>> progress through the loop,,but when I ran this code:
    >>> [code snipped]
    >>>
    >>>> I got the following results,,
    >>>>
    >>>> [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    >>>> loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    >>>> loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    >>>> loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    >>>> loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    >>>> loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]
    >>> What results did you expect (this isn't a sarcastic or rhetorical
    >>> question -- I really don't know what it is you're trying to do)? Perhaps you
    >>> meant to override the toString() method? See
    >>> http://www.javapractices.com/Topic55.cjp
    >>>
    >>> - Oliver

    >


    Please don't add your material at the top - its better to keep things in
    order.

    You are still calling the Loop object toString method, without having
    supplied a toString override in the Loop class. Loop's immediate
    superclass is Object, so you are using the Object toString method.

    You need to add to Loop a method:

    public String toString(){
    // build the string you want to describe your loop object.
    // return it.
    }

    Patricia
     
    Patricia Shanahan, Nov 9, 2006
    #6
  7. mai

    mai Guest

    Patricia Shanahan wrote:
    > mai wrote:
    > > Hi,, Thanks for your help,, but I still get the same results even after
    > > writing:
    > > System.out.println(doLoops().get(i).toString());
    > >
    > > Please help,,
    > > mai wrote:
    > >> I was trying to print the values of the list. Which are ,supposesdly,
    > >> integer values,
    > >> Oliver Wong wrote:
    > >>> "mai" <> wrote in message
    > >>> news:...
    > >>>> Hi,,
    > >>>> I'm learning Java,, and I was trying to creat a function that performs
    > >>>> a three-level nested loop and returns a list of loop objects indicating
    > >>>> progress through the loop,,but when I ran this code:
    > >>> [code snipped]
    > >>>
    > >>>> I got the following results,,
    > >>>>
    > >>>> [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    > >>>> loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    > >>>> loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    > >>>> loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    > >>>> loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    > >>>> loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]
    > >>> What results did you expect (this isn't a sarcastic or rhetorical
    > >>> question -- I really don't know what it is you're trying to do)? Perhaps you
    > >>> meant to override the toString() method? See
    > >>> http://www.javapractices.com/Topic55.cjp
    > >>>
    > >>> - Oliver

    > >

    >
    > Please don't add your material at the top - its better to keep things in
    > order.
    >
    > You are still calling the Loop object toString method, without having
    > supplied a toString override in the Loop class. Loop's immediate
    > superclass is Object, so you are using the Object toString method.
    >
    > You need to add to Loop a method:
    >
    > public String toString(){
    > // build the string you want to describe your loop object.
    > // return it.
    > }
    >
    > Patricia


    Thanx for your message,, but shouldn't doLoops().get(i) return the
    element of the index i in the list,, that's what i understood from the
    java doc,, and I don't know how to build the string in the toString
    method :s ,, can u help me further more,, Thanx once again
     
    mai, Nov 9, 2006
    #7
  8. mai wrote:
    > Patricia Shanahan wrote:
    >> mai wrote:
    >>> Hi,, Thanks for your help,, but I still get the same results even after
    >>> writing:
    >>> System.out.println(doLoops().get(i).toString());
    >>>
    >>> Please help,,
    >>> mai wrote:
    >>>> I was trying to print the values of the list. Which are ,supposesdly,
    >>>> integer values,
    >>>> Oliver Wong wrote:
    >>>>> "mai" <> wrote in message
    >>>>> news:...
    >>>>>> Hi,,
    >>>>>> I'm learning Java,, and I was trying to creat a function that performs
    >>>>>> a three-level nested loop and returns a list of loop objects indicating
    >>>>>> progress through the loop,,but when I ran this code:
    >>>>> [code snipped]
    >>>>>
    >>>>>> I got the following results,,
    >>>>>>
    >>>>>> [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    >>>>>> loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    >>>>>> loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    >>>>>> loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    >>>>>> loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    >>>>>> loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]
    >>>>> What results did you expect (this isn't a sarcastic or rhetorical
    >>>>> question -- I really don't know what it is you're trying to do)? Perhaps you
    >>>>> meant to override the toString() method? See
    >>>>> http://www.javapractices.com/Topic55.cjp
    >>>>>
    >>>>> - Oliver

    >> Please don't add your material at the top - its better to keep things in
    >> order.
    >>
    >> You are still calling the Loop object toString method, without having
    >> supplied a toString override in the Loop class. Loop's immediate
    >> superclass is Object, so you are using the Object toString method.
    >>
    >> You need to add to Loop a method:
    >>
    >> public String toString(){
    >> // build the string you want to describe your loop object.
    >> // return it.
    >> }
    >>
    >> Patricia

    >
    > Thanx for your message,, but shouldn't doLoops().get(i) return the
    > element of the index i in the list,, that's what i understood from the
    > java doc,, and I don't know how to build the string in the toString
    > method :s ,, can u help me further more,, Thanx once again
    >


    Are you following a course, tutorial, or book? This exercise, in its
    current form, should not appear until after you know a little string
    manipulation and have seen some toString examples.

    An alternative way is to forget about printing Loop objects directly,
    and instead print the fields, for example doLoops().get(i).level.

    Patricia
     
    Patricia Shanahan, Nov 9, 2006
    #8
  9. mai

    Daniel Pitts Guest

    mai wrote:
    > Patricia Shanahan wrote:
    > > mai wrote:
    > > > Hi,, Thanks for your help,, but I still get the same results even after
    > > > writing:
    > > > System.out.println(doLoops().get(i).toString());
    > > >
    > > > Please help,,
    > > > mai wrote:
    > > >> I was trying to print the values of the list. Which are ,supposesdly,
    > > >> integer values,
    > > >> Oliver Wong wrote:
    > > >>> "mai" <> wrote in message
    > > >>> news:...
    > > >>>> Hi,,
    > > >>>> I'm learning Java,, and I was trying to creat a function that performs
    > > >>>> a three-level nested loop and returns a list of loop objects indicating
    > > >>>> progress through the loop,,but when I ran this code:
    > > >>> [code snipped]
    > > >>>
    > > >>>> I got the following results,,
    > > >>>>
    > > >>>> [loop.Loop@10b62c9, loop.Loop@82ba41, loop.Loop@923e30,
    > > >>>> loop.Loop@130c19b, loop.Loop@1f6a7b9, loop.Loop@7d772e,
    > > >>>> loop.Loop@11b86e7, loop.Loop@35ce36, loop.Loop@757aef,
    > > >>>> loop.Loop@d9f9c3, loop.Loop@9cab16, loop.Loop@1a46e30,
    > > >>>> loop.Loop@3e25a5, loop.Loop@19821f, loop.Loop@addbf1, loop.Loop@42e816,
    > > >>>> loop.Loop@9304b1, loop.Loop@190d11, loop.Loop@a90653, loop.Loop@de6ced]
    > > >>> What results did you expect (this isn't a sarcastic or rhetorical
    > > >>> question -- I really don't know what it is you're trying to do)? Perhaps you
    > > >>> meant to override the toString() method? See
    > > >>> http://www.javapractices.com/Topic55.cjp
    > > >>>
    > > >>> - Oliver
    > > >

    > >
    > > Please don't add your material at the top - its better to keep things in
    > > order.
    > >
    > > You are still calling the Loop object toString method, without having
    > > supplied a toString override in the Loop class. Loop's immediate
    > > superclass is Object, so you are using the Object toString method.
    > >
    > > You need to add to Loop a method:
    > >
    > > public String toString(){
    > > // build the string you want to describe your loop object.
    > > // return it.
    > > }
    > >
    > > Patricia

    >
    > Thanx for your message,, but shouldn't doLoops().get(i) return the
    > element of the index i in the list,, that's what i understood from the
    > java doc,, and I don't know how to build the string in the toString
    > method :s ,, can u help me further more,, Thanx once again


    doLoops().get(i) will return the element at index i. the element at
    index I is a "Loop" object, not an integer.

    List<Loop> doLoops; // This declares a list of loops.

    List<Integer> integers; // This is a list of Integer objects.
     
    Daniel Pitts, Nov 9, 2006
    #9
  10. mai

    Oliver Wong Guest

    "mai" <> wrote in message
    news:...
    >
    > Patricia Shanahan wrote:
    >
    >> public String toString(){
    >> // build the string you want to describe your loop object.
    >> // return it.
    >> }
    >>

    [...]
    > I don't know how to build the string in the toString
    > method :s ,, can u help me further more,, Thanx once again


    Add the following method to your Loop object:

    public String toString(){
    return "Hello world!";
    }

    and run your program again, and observe the results. That should give you a
    hint as to how to implement your toString() method.

    - Oliver
     
    Oliver Wong, Nov 9, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    603
    benben
    Jan 31, 2006
  2. 7stud
    Replies:
    11
    Views:
    700
    Dennis Lee Bieber
    Mar 20, 2007
  3. Nico Grubert
    Replies:
    1
    Views:
    331
    Peter Otten
    Jan 10, 2008
  4. Roedy Green
    Replies:
    3
    Views:
    437
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    390
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page