Nested loop structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • X106
    New Member
    • Oct 2007
    • 5

    Nested loop structure

    hi
    i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

    55551
    55512
    55123
    51234
    12345


    I need a code to make above mentioned , in nested loop form.

    thanks and regards
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by X106
    hi
    i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

    55551
    55512
    55123
    51234
    12345


    I need a code to make above mentioned , in nested loop form.

    thanks and regards
    Try to figure out the regularity: Java counts starting at zero, so there are lines
    0, 1, 2, 3 and 4. On line i (i in [0,4]) there are i+1 '5' characters followed by the
    digits i+1, ... i+2 etc. until 5 characters are printed.

    That observation leeds to a nested loop: the outer loop simply counts from 0 to 4.
    The inner loop also counts from 0 to 4 and uses an if statement to decide whether
    or not to print 5s or simply increasing digits 1, 2, 3 etc. Of course there are many
    ways to solve this little puzzle.

    kind regards,

    Jos

    Comment

    • ajos
      Contributor
      • Aug 2007
      • 283

      #3
      Originally posted by X106
      hi
      i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

      55551
      55512
      55123
      51234
      12345


      I need a code to make above mentioned , in nested loop form.

      thanks and regards
      i'll give you an example, i hope my fellow TSDN's members wont flame at me--->
      Code:
      <%@ page import="java.util.*" %>
      <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
      <html>
      <head><title></title></head>
      <body>
      <%
      ArrayList arrList = new ArrayList() ;
      ArrayList subList1 = new ArrayList() ;
      ArrayList subList2 = new ArrayList() ;
      ArrayList subList3 = new ArrayList() ;
      
      subList1.add( "subList1_1" ) ;
      subList1.add( "subList1_2" ) ;
      subList1.add( "subList1_3" ) ;
      subList1.add( "subList1_4" ) ;
      
      subList2.add( "subList2_1" ) ;
      subList2.add( "subList2_2" ) ;
      subList2.add( "subList2_3" ) ;
      subList2.add( "subList2_4" ) ;
      
      subList3.add( "subList3_1" ) ;
      subList3.add( "subList3_2" ) ;
      subList3.add( "subList3_3" ) ;
      subList3.add( "subList3_4" ) ;
      
      arrList.add( subList1 ) ;
      arrList.add( subList2 ) ;
      arrList.add( subList3 ) ;
      
      request.setAttribute("myList", arrList );
      %>
      
      <c:forEach var='mainItem' items='${requestScope.myList}' varStatus='status'>
            Main ArrayList Count: <c:out value='${status.count}'/> <br />
            <c:forEach var='subItem' items='${mainItem}'>
                  <font color="red">Names: <c:out value='${subItem}'/></font>
                  <br />
            </c:forEach>
      
      </c:forEach>
      </body>
      </html>

      Comment

      • ajos
        Contributor
        • Aug 2007
        • 283

        #4
        Originally posted by ajos
        i'll give you an example, i hope my fellow TSDN's members wont flame at me--->
        Code:
        <%@ page import="java.util.*" %>
        <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
        <html>
        <head><title></title></head>
        <body>
        <%
        ArrayList arrList = new ArrayList() ;
        ArrayList subList1 = new ArrayList() ;
        ArrayList subList2 = new ArrayList() ;
        ArrayList subList3 = new ArrayList() ;
        
        subList1.add( "subList1_1" ) ;
        subList1.add( "subList1_2" ) ;
        subList1.add( "subList1_3" ) ;
        subList1.add( "subList1_4" ) ;
        
        subList2.add( "subList2_1" ) ;
        subList2.add( "subList2_2" ) ;
        subList2.add( "subList2_3" ) ;
        subList2.add( "subList2_4" ) ;
        
        subList3.add( "subList3_1" ) ;
        subList3.add( "subList3_2" ) ;
        subList3.add( "subList3_3" ) ;
        subList3.add( "subList3_4" ) ;
        
        arrList.add( subList1 ) ;
        arrList.add( subList2 ) ;
        arrList.add( subList3 ) ;
        
        request.setAttribute("myList", arrList );
        %>
        
        <c:forEach var='mainItem' items='${requestScope.myList}' varStatus='status'>
              Main ArrayList Count: <c:out value='${status.count}'/> <br />
              <c:forEach var='subItem' items='${mainItem}'>
                    <font color="red">Names: <c:out value='${subItem}'/></font>
                    <br />
              </c:forEach>
        
        </c:forEach>
        </body>
        </html>
        try to manipulate with this example.

        Comment

        • X106
          New Member
          • Oct 2007
          • 5

          #5
          I understand the concept, jos,but I can't write in code, I just wrike like that

          class NestedLoop {
          public static void main (String [] a) {
          int n=5, i, j;
          for (i = 1; i <=n; i++) {

          Then I don't know how to write.Can help me how to write in code?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by X106
            I understand the concept, jos,but I can't write in code, I just wrike like that

            class NestedLoop {
            public static void main (String [] a) {
            int n=5, i, j;
            for (i = 1; i <=n; i++) {

            Then I don't know how to write.Can help me how to write in code?
            Write the loops that Jos was explaining above.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              Write the loops that Jos was explaining above.
              ... or go fancy and write a single loop:

              [code=java]
              private static void square(int n) { // assume 1 <= n <= 9
              char[] line= new char[n];
              Arrays.fill(lin e, (char)('0'+n));
              String s= new String(line);
              for (int i= 1; i <= n; i++)
              System.out.prin tln(s= (s+i).substring (1));
              }
              [/code]

              You can't turn this in because there's no nested loop :-P

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by JosAH
                .....

                You can't turn this in because there's no nested loop :-P

                kind regards,

                Jos
                Yep, but keep the ideas in mind for the time when you are no longer writing code for school only.

                Comment

                • X106
                  New Member
                  • Oct 2007
                  • 5

                  #9
                  Thank you ,you all help me a lot!!!!!!!!!!

                  Comment

                  Working...