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

Thread: About Array

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    India
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default About Array

    Hello Experts!

    I am new to this forum.
    Could you please tell me how this multi-dimensional array works? How it corresponds to differents style of outputs format like triangle shape of elements output?
    class TwoDAgain {
    public static void main(String args[]) {
    int twoD[][] = new int[4][];
    twoD[0] = new int[1];
    twoD[1] = new int[2];
    twoD[2] = new int[3];
    twoD[3] = new int[4];
    int i, j, k = 0;
    for(i=0; i<4; i++)
    for(j=0; j<i+1; j++) {
    twoD[i][j] = k;
    k++;
    }
    for(i=0; i<4; i++) {
    for(j=0; j<i+1; j++)
    System.out.print(twoD[i][j] + " ");
    System.out.println();
    }
    }
    }
    output:
    0
    1 2
    3 4 5
    6 7 8 9

    Regards
    Kaan


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

    Default Re: About Array

    how this multi-dimensional array works
    Multidim arrays are really a grouping of single dim arrays.
    Each dim of the array can contain an array of a unique dimensions.

    int twoD[][] = new int[4][];  // Create 2 dim array with 4 slots in the first dim
    twoD[0] = new int[1];          //  set the first array to be a one dim array with 1 slot
    twoD[1] = new int[2];          //  set the second array to be a one dim with 2 slots
    twoD[2] = new int[3];         //  set the third array to be a one dim with 3 slots
    twoD[3] = new int[4];         //  set the forth array to be a one dim with 4 slots
    The valid indexes for the above array would be:
    0,0
    1,0 1,1
    2,0 2,1 2,2
    3,0 3,1 3,2 3,3

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: About Array

    Hello and welcome.

    In future, please post questions in the correct forum. Moved to - Collections and Generics

    I have also added the highlight tags to your code. See my signature.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. copying 2dim array into 1 dim array
    By av8 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 29th, 2011, 10:59 PM
  2. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM