Q

Defining a Java array

How do you define a Java array whose entries are also Java arrays?

The easiest method is to define a two-dimensional array and assign each member as follows:

// define two-dimensional array

    Object[][] objArr = new Object[5][3];

 

    // assign values to array

    for (int i = 0; i < objArr.length; i++)

    {

      objArr[i] = new Integer[3];

      for (int j = 0; j < objArr[i].length; j++)

      {

        objArr[i][j] = new Integer((i + 1) * (j + 5));

      }

    }

 

    // display results

    for (int i = 0; i < objArr.length; i++)

    {

      for (int j = 0; j < objArr[i].length; j++)

      {

        System.out.println("objArr[" + i + "][" + j + "] = " + objArr[i][j]);

      }

    }

This was last published in February 2005

Dig Deeper on Java Web Services

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close