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 2 of 2

Thread: Output of 2 identical two-dimensional arrays print their tables differently

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Output of 2 identical two-dimensional arrays print their tables differently

    I have 2 two-dimensional arrays of integers (the same numbers are placed in the same index spots). They're both using enhanced for loops to print their tables, but they don't print the same.
    package randomExperiments;
     
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Random;
    import java.lang.StringBuilder;
    import java.text.DecimalFormat;
     
    import static randomExperiments.StaticMethodExample.*;
     
    public class RandomExperiments {
     
    	public static void main(String[] args){
     
    		//Two dimensional array:
    		int rowCount = 2, columnCount = 4;
    		int[][] numsTable = new int[rowCount][columnCount];
    		numsTable[0][0] = 9;
    		numsTable[0][1] = 7;
    		numsTable[0][2] = 0;
    		numsTable[0][3] = 3;
    		numsTable[1][0] = 4;
    		numsTable[1][1] = 0;
    		numsTable[1][2] = 3;
    		numsTable[1][3] = 1;
    		//In one statement:
    		int[][] numsTableAgain = { {9,7}, {0,3}, {4,0}, {3,1} };
     
    		for(int[] row : numsTable) {
    			for(int column : row) {//for each column of the current row
    				System.out.print("[" + column + "]" + " ");
    			}
    			System.out.println("\n");
    		}
    		for(int[] row : numsTableAgain) {
    			for(int column : row) {//for each column of the current row
    				System.out.print("[" + column + "]" + " ");
    			}
    			System.out.println("\n");
    		}		
    	}
    }

    The output in the console looks like this:

    [9] [7] [0] [3]

    [4] [0] [3] [1]

    [9] [7]

    [0] [3]

    [4] [0]

    [3] [1]


    Why does one print a 2x4 table, and the other print a 4x2 table? Is it possible to make them both print a 2x4 table?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,781
    Thanks
    52
    Thanked 2,329 Times in 2,285 Posts

    Default Re: Output of 2 identical two-dimensional arrays print their tables differently

    make them both print a 2x4 table?
    Change numsTableAgain so that it is a 2x4 array instead of being a 4x2 array.
    In other words, it should have 2 rows with 4 elements in each row.
    Now it has 4 rows with 2 elements in each row.

    To see another representation of a 2 dim array use the Arrays class's deepToString method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. 2-dimensional arrays
    By johnmerlino in forum Java Theory & Questions
    Replies: 3
    Last Post: August 9th, 2012, 02:15 PM
  2. Help with 2 Dimensional Arrays
    By s1mmi in forum Object Oriented Programming
    Replies: 11
    Last Post: February 7th, 2012, 01:43 PM
  3. Two dimensional Arrays
    By masterT in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 19th, 2011, 06:37 PM
  4. HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2011, 04:44 PM
  5. Two dimensional arrays - HELP!
    By ecco in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 19th, 2010, 01:32 PM