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 used arrays already, in the C Language. This section discusses arrays in Java

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.
The following program,
The output from this program is:
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as
Similarly, you can declare arrays of other types:
You can also place the square brackets after the array's name:
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
If this statement were missing, the compiler would print an error like the following, and compilation would fail:
The next few lines assign values to each element of the array:
Each array element is accessed by its numerical index:
Alternatively, you can use the shortcut syntax to create and initialize an array:
Here the length of the array is determined by the number of values provided between { and }.
You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as
In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length(RAGGED/JAGGED ARRAYS) , as shown in the following
The output from this program is:
Finally, you can use the built-in
will print the array's size to standard output.
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.
The following program,
ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output.class ArrayDemo {
public static void main(String[] args) {
// declares an array of integers
int[] anArray;
// allocates memory for 3 integers
anArray = new int[3];
// initialize first element
anArray[0] = 100;
// initialize second element
anArray[1] = 200;
// etc.
anArray[2] = 300;
System.out.println("Element at index 0: "
+ anArray[0]);
System.out.println("Element at index 1: "
+ anArray[1]);
System.out.println("Element at index 2: "
+ anArray[2]);
}
}
Element at index 0: 100 Element at index 1: 200 Element at index 2: 300
In a real-world programming situation, you'd probably use one of the supported looping
constructs to iterate through each element of the array, rather than write each line individually as shown above. However, this example clearly illustrates the
array syntax.
Declaring a Variable to Refer to an Array
The above program declaresanArray with the following line of code:// declares an array of integers int[] anArray;
type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions as previously discussed in the naming section. As with variables of other types, the declaration does not actually create an array — it simply tells the compiler that this variable will hold an array of the specified type.Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings;
// this form is discouraged float anArrayOfFloats[];
Creating, Initializing, and Accessing an Array
One way to create an array is with thenew operator. The next statement in the ArrayDemo program allocates an array with enough memory for ten integer elements and assigns the array to theanArray variable.// create an array of integers anArray = new int[10];
ArrayDemo.java:4: Variable anArray may not have been initialized.
anArray[0] = 100; // initialize first element anArray[1] = 200; // initialize second element anArray[2] = 300; // etc.
System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);
int[] anArray = {
100, 200, 300,
400, 500, 600,
700, 800, 900, 1000
};
You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as
String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length(RAGGED/JAGGED ARRAYS) , as shown in the following
MultiDimArrayDemoprogram:class MultiDimArrayDemo {
public static void main(String[] args) {
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};
// Mr. Smith
System.out.println(names[0][0] + names[1][0]);
// Ms. Jones
System.out.println(names[0][2] + names[1][1]);
}
}
Mr. Smith Ms. Jones
length property to determine the size of any array. The codeSystem.out.println(anArray.length);
No comments:
Post a Comment