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

Thread: 2-dimensional arrays

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 2-dimensional arrays

    In this code snippet:

    public static int[][] pascalTriangle(int n) {
      int[][] pt = new int[n][];
      for (int i = 0; i < n; i++) {
         pt[i] = new int[i + 1];                   
         pt[i][0] = 1;                              
         for (int j = 1; j < i; j++) {
           pt[i][j] = pt[i - 1][j - 1] + pt[i - 1][j]; 
         }
         pt[i][i] = 1;         
       }
       return pt;
    }

    in the second live, we specify a length for the one dimesnion, but for the second dimension, its just open and close brackets. How is it possible to do that, to be able to declare a two-dimensional array like that without specifying a length? I thought an array had to have an exact length when initialized before you could actually assign items to it.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    970
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: 2-dimensional arrays

    pt is an array whose members are (or will be) arrays of int values. You can think of it as a 2d array, but it is an array whose elements are themselves arrays.

    And when you initialised pt you did specify a length: its length is n. You can try "System.out.println(pt.length);". What you didn't specify - and don't have to - is the length of the arrays which are the elements of pt. It is only when you say:

    pt[i] = new int[i + 1];

    that you give these so called "inner" arrays a length. And notice that the inner arrays (the elements of the pt array) do not all have to be the same length. In your case they are not all the same length.

    A double array in Java differs from, say, a matrix in maths where there is a definite number of elements in each direction. A better mental image might be a page of text. The page consists of an array of lines. And each line consists of an array of characters. But even once you've said how many lines there are, you haven't said how long the lines will be - and they can all be different lengths. Sometimes people borrow the term "ragged" from printed text to describe the corresponding arrays in Java.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    johnmerlino (August 9th, 2012)

  4. #3
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: 2-dimensional arrays

    You can also imagine it as a triangular array

  5. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 2-dimensional arrays

    that was helpful, thanks

Similar Threads

  1. 2 Dimensional Arrays: I cant manage to read/write them.
    By seaofFire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 02:32 PM
  2. How to begin with multi-dimensional arrays
    By Lorelai in forum Java Theory & Questions
    Replies: 1
    Last Post: April 6th, 2012, 07:56 PM
  3. Help with 2 Dimensional Arrays
    By s1mmi in forum Object Oriented Programming
    Replies: 11
    Last Post: February 7th, 2012, 01:43 PM
  4. Two dimensional Arrays
    By masterT in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 19th, 2011, 06:37 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

Tags for this Thread