Results 1 to 6 of 6

Thread: Array question

  1. #1
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default Array question

    Imagine I had the following:

    Java Code:
    int[][] myArray={{1,2,3},{4,5,6}};
    Does this give me a matrix of 2 rows and 3 cols or 3 cols and 2 rows? Or to put it another way if I did:

    [code]int myAnswer=myArray[1][1][code]

    What value would I get for myAnswer?

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    2 rows and 3 column. myArray[1][1] would give you the same answer regardless if it's 3 column & 2 rows or 3 rows & 2 columns: 5

    Let's say you did:
    Java Code:
    int myAnswer = myArray[1][0]
    you would get: 4
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by TaxpayersMoney View Post
    Imagine I had the following:

    Java Code:
    int[][] myArray={{1,2,3},{4,5,6}};
    Does this give me a matrix of 2 rows and 3 cols or 3 cols and 2 rows? Or to put it another way if I did:

    [code]int myAnswer=myArray[1][1][code]

    What value would I get for myAnswer?
    Java is (just as all C-like languages) row oriented w.r.t. arrays. Try it:

    Java Code:
    int[][] myArray= { { 1, 2, 3 }, { 4, 5, 6 } };
    printRow(myArray[0]);
    printRow(myArray[1]);
    ...
    private static void printRow(int[] row) {
       System.out.println(Arrays.toString(row));
    }
    AFAIK only Fortran stores its matrixes in a column oriented way ...

    kind regards,

    Jos

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by TaxpayersMoney View Post
    What value would I get for myAnswer?

    look at the following code

    Java Code:
    public class ArrayOutput {
    	public static void main(String[] args) {
    		int[][] myArray = { { 1, 2, 3 }, { 4, 5, 6 } };
    
    		for (int i = 0; i < myArray.length; i++) {
    			for (int j = 0; j < myArray[i].length; j++)
    				System.out.println("Array[" + i + "][" + j + "] = "
    						+ myArray[i][j]);
    		}
    	}
    }

    that will output each element in the array

    Array[0][0] = 1
    Array[0][1] = 2
    Array[0][2] = 3
    Array[1][0] = 4
    Array[1][1] = 5
    Array[1][2] = 6

    now, are you able to guess the output of this array?

    int[][] myArray = { { 1 }, { 4, 5, 6 }, {2, 3}};
    Last edited by j2me64; 06-09-2010 at 01:08 PM.

  5. #5
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks everyone.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Or you can just think of a multidimensional array as an array inside of an array which is what it is.

    or even better you can reduce complexity i.e int[][] myArray = { { 1 }, { 4, 5, 6 }, {2, 3}}; never making an array like this!!lol
    Last edited by al_Marshy_1981; 06-11-2010 at 01:43 AM.

Similar Threads

  1. Array Question
    By sc001 in forum New To Java
    Replies: 1
    Last Post: 02-14-2010, 04:57 AM
  2. need help with question(method & array)
    By highschool in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 05:06 PM
  3. array question
    By dazednconfused in forum New To Java
    Replies: 4
    Last Post: 09-15-2009, 05:44 AM
  4. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 AM
  5. Basic array question
    By jigglywiggly in forum New To Java
    Replies: 12
    Last Post: 01-09-2009, 04:44 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
  •