Results 1 to 5 of 5
  1. #1
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default Two dimensional array

    Java Code:
    char[ ][ ] arrayTwo; 
    		arrayTwo = new char [1][ ];
    		arrayTwo[0] = new char [3]; 
    		arrayTwo[1] = new char [4]; 
    		
    		arrayTwo[0][2] ='z';
    I guess this is a simple question but I fail to find the answers in my books.

    I want to know, what "arrayTwo[0] = new char [3]" means. So I declared a two dimensional array called "ArrayTwo" which contains two rows. But by declaring "arrayTwo[0] = new char [3]", does it mean that "the first row will have 3 columns"??? Or does it mean something else?

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

    Default

    Quote Originally Posted by niu_niu View Post
    I want to know, what "arrayTwo[0] = new char [3]" means. So I declared a two dimensional array called "ArrayTwo" which contains two rows. But by declaring "arrayTwo[0] = new char [3]", does it mean that "the first row will have 3 columns"???

    exactly. this way you can declare a different number of elements/columns per row.

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I think you meant

    Java Code:
    char[ ][ ] arrayTwo; 
    arrayTwo = new char [2][ ];
    arrayTwo[0] = new char [3]; 
    arrayTwo[1] = new char [4]; 
    		
    arrayTwo[0][2] ='z';
    You get a null pointer with that code because you are trying to assign to row two but you only made one row.

    And yes, as j2me64 says, you can declare different array lengths for a 2d array. You can even do it for 3d arrays if you want - although thats will start to hurt your brain.

    Berkeleybross

  4. #4
    niu_niu is offline Member
    Join Date
    Jun 2010
    Posts
    48
    Rep Power
    0

    Default

    every time I am stuck in the java pot, my brain goes to 100F...but thanks for pouring the cool water in!

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Well seeing as the human body is 98.6F i dont think 100F will do too much harm ;)

    Glad to help though
    Berkeleybross

Similar Threads

  1. 2 dimensional array help!
    By ber1023 in forum New To Java
    Replies: 9
    Last Post: 01-02-2011, 12:29 AM
  2. about two dimensional array
    By matin1234 in forum New To Java
    Replies: 2
    Last Post: 06-01-2010, 11:09 AM
  3. Mulit dimensional Array
    By jonsamwell in forum New To Java
    Replies: 7
    Last Post: 02-03-2009, 12:35 PM
  4. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 07:21 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 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
  •