View Single Post
Old 03-12-2012, 03:18 AM   PM User | #1
b00mtastik
New to the CF scene

 
Join Date: Mar 2012
Location: In your shadow.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
b00mtastik is an unknown quantity at this point
Exclamation Unexpected Output from a 2D Array

Hey guys, B00mer here.

So, was just coding and messing around with 2 dimensional arrays to kinda get a better understanding of it and also to be able to manipulate it and do some other stuff with it. However, upon compiling and running the 2D array to output all its contents after it had already been populated during compile, it game me some crazy numbers, instead of the ones I was looking for. And I for the life of me cannot understand why. So I thought you guys might be able to help. The simple code is as follows:

Code:
public class nbyn {
   
    public static void main(String[] args) {
        
        //Array Box A
        int [][] a = new int[][]{
            { '1', '2', '3' },
            { '4', '5', '6' },
            { '7', '8', '9' },
        };
        
        //Array Box B - Ignore this one for now, nothing happening with it
        int [][] b = new int[][]{
            { '1', '2', '3' },
            { '4', '5', '6' },
            { '7', '8', '9' },
        };
        
        int i, j;
        //traverse the array 'a' and output all its contents
        for (i=0; i < a.length; i++) {
            for (j=0; j < a[i].length; j++) {
                System.out.print(" " + a[i][j]);
            }
            System.out.println("");
        }  
    }  
}
So the output I was expecting was:
Code:
1, 2, 3, 4, 5, 6, 7, 8, 9
Instead I get this:
Code:
run:
 49 50 51
 52 53 54
 55 56 57
BUILD SUCCESSFUL (total time: 0 seconds)
Any explanation of how this output came to being, and also a bit of help on what I NEED to do in order to get the output I want, would be amazingtastik.

Danke.

-B00mer
b00mtastik is offline   Reply With Quote