To refer to a particular element of an array, simply place the index of the desired element in square brackets after the name of the array. For example:
In some programming languages, such as C and C++, it is a common bug to write code that tries to read or write array elements that are past the end of the array. Java does not allow this. Every time you access an array element, the Java interpreter automatically checks that the index you have specified is valid. If you specify a negative index or an index that is greater than the last index of the array, the interpreter throws an exception of type ArrayIndexOutOfBoundsException. This prevents you from reading or writing nonexistent array elements.String[] responses = new String[2]; // Create an array of two strings responses[0] = "Yes"; // Set the first element of the array responses[1] = "No"; // Set the second element of the array // Now read these array elements System.out.println(question + " (" + responses[0] + "/" + responses[1] + " ): ");
Array index values are integers; you cannot index an array with a floating-point value, a boolean, an object, or another array. charvalues can be converted to int values, so you can use characters as array indexes. Although long is an integer data type, long values cannot be used as array indexes. This may seem surprising at first, but consider that an int index supports arrays with over two billion elements. An int[] with this many elements would require eight gigabytes of memory. When you think of it this way, it is not surprising that long values are not allowed as array indexes.
Besides setting and reading the value of array elements, there is one other thing you can do with an array value. Recall that whenever we create an array, we must specify the number of elements the array holds. This value is referred to as the length of the array; it is an intrinsic property of the array. If you need to know the length of the array, append .length to the array name:
.length is special Java syntax for arrays. An expression like a.length looks as though it refers to a field of an object a, but this is not actually the case. The .length syntax can be used only to read the length of an array. It cannot be used to set the length of an array (because, in Java, an array has a fixed length that can never change).if (errorCode < errorMessages.length) System.out.println(errorMessages[errorCode]);
In the previous example, the array index within square brackets is a variable, not an integer literal. In fact, arrays are most often used with loops, particularly for loops, where they are indexed using a variable that is incremented or decremented each time through the loop:
In Java, the first element of an array is always element number 0. If you are accustomed to a programming language that numbers array elements beginning with 1, this will take some getting used to. For an array a, the first element is a[0], the second element is a[1], and the last element is:int[] values; // Array elements initialized elsewhere int total = 0; // Store sum of elements here for(int i = 0; i < values.length; i++) // Loop through array elements total += values[i]; // Add them up
a[a.length - 1] // The last element of any array named a
No comments:
Post a Comment