CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Arrays

Arrays
Two-dimensional arrays
Introduction

Two-dimensional arrays:

  • Have Two or more columns of values
  • Have Rows and columns
  • Use two subscripts
  • Are called a matrix or table

The syntax for creating a two-dimensional array

type[][] arrayName;

The syntax for initiating a two-dimensional array object

type[][] arrayName = new type[rowCount][columnCount];

The syntax for accessing an element of a two-dimensional array

arrayName [rowIndex][columnIndex];

Length of a two-dimensional array

The length field holds the number of rows in the array.

arrayName.length;
Each row has a length field that holds the number of columns in the row.
arrayName[rowIndex].length;

Example

int[][] x = new int[3][4];

Three ways to process two-dimensional arrays:

  • Entire array
  • Particular row of array (row processing)
  • Particular column of array (column processing)

Previous | Introduction | Rectangular | Jagged