Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Multi dimensional array

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multi dimensional array

    I am trying to write a code for multidimensional array, allocate memory for variables and access the value of the arrays. I want them to be shown as rows and columns. but my code only shows one column and no rows.
    here is my code:
    public static void main(String[] args) {
    int[ ][ ] aryNumbers = new int[2][2];
    aryNumbers [0][0] = 1;
    aryNumbers [0][1] = 2;
    aryNumbers [1][0] = 3;
    aryNumbers [1][1] = 4;
    int rows = 2;
    int columns = 2;
    int i,j;
    for(i=0;i<rows;i++){
    for(j=0;j<columns;j++){

    System.out.println(aryNumbers[i][j] + " ");
    }
    System.out.println(" ");
    }

    the output i get is:
    1
    2
    3
    4
    the output i want is:
    1 2
    3 4

    Any help would be really appreciated. thanks in advance


  2. #2
    Junior Member
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multi dimensional array

    System.out.println will always add a carriage return on the end. Use printf instead.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multi dimensional array

    wow! that worked, thanks
    i am not sure though when to use printf and when to use println

    --- Update ---

    can i assign the values in multidimensional array like this:
    int [][] aryNumbers = {{10,12,43,11,22},{20,45,56,1,33},{30,67,32,14,44} ,{40,12,87,14,55}{50,86,66,13,66},{60,53,44,12,11} };

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multi dimensional array

    Long story short, user printf when you don't want to add a newline at the end, use println when you do. Or just use printf always and manually add the newline character to your string.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,944
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Multi dimensional array

    There is also the print() method that does not add a newline.

    When posting code, please wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Multi dimensional array

    Quote Originally Posted by blab View Post
    Use printf instead.
    I'd have to agree with Norm. The printf method is for formatting the output. If all you what is to display something without a carriage return then use the print method.
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multi dimensional array

    Thank you very much for the replies Junky and Norm.
    I am very glad to be here in this forum in my beginning phase of java learning process.

Similar Threads

  1. How to define a multi-dimensional array?
    By javaiscool in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2013, 11:40 PM
  2. Convert ArrayList to multi-dimensional array
    By dougie1809 in forum Loops & Control Statements
    Replies: 4
    Last Post: April 5th, 2013, 12:01 PM
  3. Searching and displaying results in a multi-dimensional array.
    By wfalcon2012 in forum Collections and Generics
    Replies: 2
    Last Post: February 18th, 2012, 07:06 PM
  4. How can I create a jigsaw puzzle with Array multi-dimensional?
    By ivan8 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 4th, 2011, 07:13 PM
  5. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 12th, 2010, 11:44 PM

Tags for this Thread