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

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

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    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
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 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.

  3. #3
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

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

    I'm having another array printing issue. My attempt for this program was to prove to both the user and the program that the array copied successfully:
    package samsExperiments;
     
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Random;
     
    import static samsExperiments.StaticMethodExample.*;
     
    import java.lang.StringBuilder;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.InputMismatchException;
     
    import customExceptions.IntegerOutOfRangeException;
     
    public class SamsExperimentsMain {
     
    	public static void main(String[] args){
     
    		int[] myInts = {5,5,8,0,7,4,1,1};
    		System.out.println("The original int array numbers are:");
    		for(int i = 0; i < myInts.length; i++) {
    			if(i == myInts.length) {
    				System.out.println(myInts[i]);//print last value without a comma in front of it
    			}
    			else {
    				System.out.print(myInts[i] + ", ");
    			}
    		}
     
    		System.out.println();
     
    		System.out.println("The numbers of the copy of that int array are:");
    		int[] copyOfMyInts = Arrays.copyOf(myInts, 8);
    		for(int i = 0; i < copyOfMyInts.length; i++) {
    			if(i == copyOfMyInts.length) {
    				System.out.println(copyOfMyInts[i]);//print last value without a comma in front of it
    			}
    			else {
    				System.out.print(copyOfMyInts[i] + ", ");
    			}			
    		}
    		System.out.println("We will now test if our program also saw that the arrays are both the same:");
    		if(Arrays.equals(myInts, copyOfMyInts)) {
    			System.out.println("Array myInts successfully copied.");
    		}
    		else {
    			System.out.println("Sorry, the values don't match. copyOf failed.");
    		}		
    	}
    }

    However, the output looks like this:

    The original int array numbers are:
    5, 5, 8, 0, 7, 4, 1, 1,
    The numbers of the copy of that int array are:
    5, 5, 8, 0, 7, 4, 1, 1, We will now test if our program also saw that the arrays are both the same:
    Array myInts successfully copied.


    My question is, why does it not look like this:

    5, 5, 8, 0, 7, 4, 1, 1
    The numbers of the copy of that int array are:
    5, 5, 8, 0, 7, 4, 1, 1
    We will now test if our program also saw that the arrays are both the same:
    Array myInts successfully copied.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    why does it not look like this:
    Can you explain what it is you want to change?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

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

    Whenever you print out a series of items with delimiters (like commas) you need to take care to not print a trailing one when you print the last item.

    Regards,
    Jim

Similar Threads

  1. 2-dimensional arrays
    By johnmerlino in forum Java Theory & Questions
    Replies: 3
    Last Post: August 9th, 2012, 01: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, 05: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, 03:44 PM
  5. Two dimensional arrays - HELP!
    By ecco in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 19th, 2010, 12:32 PM