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 first published in February 2005

Dig deeper on Java Web Services

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

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.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close