Home arrow Java & J2EE arrow Page 3 - Primitive Data Types and Basic Language Rules for Java

Multidimensional Arrays - Java

Last time we discussed some basic concepts related to object-oriented programming. Two major ideas we defined were objects and variables. In this article, we will continue our discussion of variables by explaining how they hold and interact with data.

TABLE OF CONTENTS:
  1. Primitive Data Types and Basic Language Rules for Java
  2. Working with Arrays
  3. Multidimensional Arrays
  4. A Few More Rules
By: James Payne
Rating: starstarstarstarstar / 15
September 24, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If we want to get crazy (and deciding to learn any programming language is a sure indicator that you do), Multidimensional Arrays offer you a way to get there. The following code demonstrates how to declare a two-dimensional array.

Int YourTwoDimensionalArray [] [] = new int [8] [8];

What the above code does is give us an 8x8 array. Think of the way a chess board looks: a grid of 8 rows and 8 columns.




















Each of the above boxes would hold two indexes; the left index is used for the row, the right index is used for column


When you allocate memory for a multidimensional array, you only have to set memory for the left dimension. You can allocate the right dimension at a later time, as in the following code.

Int YourTwoDimensionalArray [] [] = new [5] [0]

YourTwoDimensionalArray[0] = new int [2]

YourTwoDimensionalArray[0] = new int [7]

YourTwoDimensionalArray[0] = new int [20]

YourTwoDimensionalArray[0] = new int [25]

YourTwoDimensionalArray[0] = new int [12]

Note also that if you ever need to know the length of an array the following code will print it to your screen:

System.out.println(YourArray.length);



 
 
>>> More Java & J2EE Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: