ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 16

Updated on October 16, 2019
dwachira profile image

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Lesson 16: Multi-dimensional arrays in Java

In our previous lesson about arrays in Java programming language, we have been working with arrays as a single list.

Arrays of multiple rows and columns (table), also called Multi-dimensional arrays can be implemented in Java programming language by specifying the number of rows and columns in array declaration.

This article will discuss how to implement multi-dimensional arrays in Java. Suppose we want to implement a multi-dimensional array as shown below:

Example of Multi-dimensional array (table)

^ Rows / Columns >
 
 
 
56
82
78
94
78
88
55
18
45
39
52
23
34
45
85
69
66
78
74
62
Source

The above multi-dimensional array has 5 rows and 4 columns (not counting table header). In fact, we can refer to a multi-dimensional array as a table. To define such an array in Java, we use the following statement:

int[ ][ ] myNums = new int[5][4];

Notice that in the above statement we have two sets of square brackets. The first set of square brackets is for the rows and the second set of square brackets is for the columns. To assign values into the first row as shown in the above array, we use the following statements:

myNums[0][0] = 56;

myNums[0][1] = 82;

myNums[0][2] = 78;

myNums[0][3] = 94;

The first row becomes 0 as index positions in Java start at zero, the columns moves from 0 to 3. To assign values into the second row, we use the following statements:

myNums[1][0] = 78;

myNums[1][1] = 88;

myNums[1][2] = 55;

myNums[1][3] = 18;

Insert all the values shown in the array and then write the following program to access and output the values. This program uses two FOR loops, one to loop through the rows and another to loop through the columns.

Source

In the above program, counter i moves through the rows while counter j moves through the columns. The statement: System.out.print(myNums[i][j]+” , ”); output the values on a line separated by a comma. In the outer FOR loop we have the statement: System.out.println(” ”); This statement creates a new line after every row.

Notice that in the above example, the output has zero values in rows 3, 4 and 5 because I did not assign any value for those rows in the program. Once you assign values in your program you’ll be able to see the actual values being displayed as outputs.

Here is another simple example to produce a multiplication table of 10x10. Write a new program and try it.

Source

My suggestion here is that you try more examples on arrays as arrays are integral aspect of any programming language. In the next lesson, we shall look at how we can implement and manipulate arrays of string type.

<< Lesson 15 | Lesson 17 >>

Comments

    0 of 8192 characters used
    Post Comment
    • dwachira profile imageAUTHOR

      Danson Wachira 

      8 years ago from Nairobi, Kenya

      @Cheryl J., Glad to see you here, programming is among us and we use programs every day. It doesn't hurt to know a bit of programming in today world. Thanks for the visit and comment.

    • dwachira profile imageAUTHOR

      Danson Wachira 

      8 years ago from Nairobi, Kenya

      @TToombs08, thanks for stopping by and commenting, am glad you found this article to be useful.

    • Cheryl J. profile image

      Cheryl J. 

      8 years ago from Houston, TX

      Very interesting information on programming. I will practice your concepts. A great hub and charts.

    • TToombs08 profile image

      Terrye Toombs 

      8 years ago from Somewhere between Heaven and Hell without a road map.

      A very interesting hub on programming in Java, dwachira. I hope I never have to, but if I find the need or desire, I will be sure to refer to your very helpful and thorough instructions.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)