An
array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the
main
method of the "Hello World!" application. This section discusses arrays in greater detail.
An array of ten elements
Each item in an array is called an
element, and each element is accessed by its numerical
index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.
public class MultiArray {
// Declare constants
final static int ROWS = 10;
final static int COLS = 5;
public static void main(String[] args) {
// Local varaibles
int rowCount;
int colCount;
int totalSize;
// Declare and allocate an array of bytes
byte[][] screenPix = new byte[ROWS][COLS];
// Obtain and store array dimensions
rowCount = screenPix.length;
colCount = screenPix[COLS].length;
totalSize = rowCount * colCount;
// To obtain the total number of elements of a
// two-dimensional ragged array you need to get the size of
// each array dimension separately
// Display array dimensions
System.out.println("Array row size: " + rowCount);
System.out.println("Array column size: " + colCount);
System.out.println("Total size: " + totalSize);
// First allocate the rows of an array
byte[][] raggedArray = new byte[5][];
// Now allocate the columns
raggedArray[0] = new byte[2];
raggedArray[1] = new byte[2];
raggedArray[2] = new byte[4];
raggedArray[3] = new byte[8];
raggedArray[4] = new byte[3];
//************************************
// static array initialization
//************************************
byte[][] smallArray = { { 10, 11, 12, 13 }, { 20, 21, 22, 23 },
{ 30, 31, 32, 33 }, { 40, 41, 42, 43 }, };
// Display the array element at row 2, column 3
System.out.println(smallArray[1][2]); // Value is 21
}
}
for more problem to see click here. |
|
0 comments
Thanks for your comment