Results 1 to 5 of 5
  1. #1
    kedecr is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Array or for loop? or both?

    I want to create a simple program that executes and prints following:

    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25
    26 27 28 29 30


    I can do this with several for loops but I guess this is the easy way.
    I wonder if I can do this with Arrays? I want to do this with arrays but can't figure it out.

    Help please! Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kedecr View Post
    I want to create a simple program that executes and prints following:

    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25
    26 27 28 29 30


    I can do this with several for loops but I guess this is the easy way.
    I wonder if I can do this with Arrays? I want to do this with arrays but can't figure it out.
    You don't need arrays for that; simply print the numbers 1 ... 30, five numbers per line and that's all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    bmeu is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    For loop in the way to go, as if you went down the route of using an array, you'd more than likely use a for loop to iterate through the array anyway.
    Cheap and nasty example:

    int[] ar = new int[4];
    ar[0] = 1;
    ar[1] = 2;
    ar[2] = 3;
    ar[3] = 4;

    for (int i = 0; i < ar.length; i++) {
    System.out.println(i);
    }
    }

  4. #4
    cejay is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    i use an array if the what I'm putting doesn't always make the same order. With what you put you can just make a for loop that incraments a number by 1 and prints it on the compiler.

    Java Code:
    private void printNumbs(){
         for(int x = 1; x < 31;x++){
            printLn(x);
         }
    }
    excuse my conventions i just sort of made that on here :p. not sure if that printLn method will work ether but im pretty sure you know the right method to print a number up. just use the correct method instead of the one i put and it should work.

    But you get the idea =) hope that helped.

    just remember, there is no wrong or right way :) (well there is a wrong way... but ya) just experiment! its fun to break your code sometimes it helps you learn.

  5. #5
    kedecr is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Refer to an object from an array/for loop
    By Chro in forum New To Java
    Replies: 2
    Last Post: 11-09-2010, 06:12 PM
  2. Loop through Array in JSP
    By Robert_85 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-25-2010, 09:00 PM
  3. Finding a the max value of the array using a for loop
    By soccer_kid_6 in forum New To Java
    Replies: 1
    Last Post: 04-11-2010, 11:25 PM
  4. How do I create an array with every cycle of a loop?
    By blackhole8746 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 07:49 AM
  5. Replies: 2
    Last Post: 05-05-2008, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •