Print an array by column help.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen

    Print an array by column help.

    I have two arrays:

    int array1[][] = { { 1, 2, 3 },
    { 4, 5, 6 } };

    int array2[][] = { { 1, 2 },
    { 3 },
    { 4, 5, 6 } };

    I need to print both of them by column like this:

    1 4
    2 5
    3 6

    and

    1 3 4
    2 5
    6

    I can do the first one, but I can't seem to use array.length and array[
    row ].length to print the array by columns. Thanks.


  • Anthony Borla

    #2
    Re: Print an array by column help.


    "Stephen" <sedelblut@hotm ail.com> wrote in message
    news:jG7Db.39$S X1.12@newsread2 .news.atl.earth link.net...[color=blue]
    > I have two arrays:
    >
    > int array1[][] = { { 1, 2, 3 },
    > { 4, 5, 6 } };
    >
    > int array2[][] = { { 1, 2 },
    > { 3 },
    > { 4, 5, 6 } };
    >
    > I need to print both of them by column like this:
    >
    > 1 4
    > 2 5
    > 3 6
    >
    > and
    >
    > 1 3 4
    > 2 5
    > 6
    >
    > I can do the first one, but I can't seem to use array.length
    > and array[row].length to print the array by columns. Thanks.
    >[/color]

    Try:

    for (int row = 0; row < array1.length; ++row)
    {
    for (int col = 0; col < array1[row].length; ++col)
    System.out.prin t(array1[row][col] + ' ');
    System.out.prin tln();
    }

    I hope this helps.

    Anthony Borla


    Comment

    Working...