After initializing an array, which means after each of its members has been given a value, you can access a member of the array to get, to manipulate, or even to change, its value. To access a member of the array, you use the square brackets as we saw above. As done for normal variables, one of the reasons for accessing a member of an array would be to display its value, which can be done by passing it to System.out.print() or to System.out.println(). Here is an example: public class Exercise { public static void main(String[] args) { double[] numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 }; System.out.print(numbers[3]); } } This would produce: 2448.32 In the same way, you can access 1, a few or all members of the array. Here are examples: public class Exercise { public static void main(String[] args) { double numbers[] = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 }; System.out.println(numbers[3]); System.out.println(numbers[0]); System.out.println(numbers[2]); System.out.println(numbers[4]); System.out.println(numbers[1]); } } This would produce: 2448.32 12.44 6.28 632.04 525.38 When accessing a member to display its value, you can also use System.out.printf(). To do this, pass the appropriate % expression to the first part of the method, and access the right member in the second part, based on its index. Here is an example: public class Exercise { public static void main(String[] args) { double[] numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 }; System.out.printf("%f", numbers[1]); } }
We saw how you can use the square brackets to access each member of the array one at a time. That technique allows you to access one, a few, or each member. If you plan to access all members of the array instead of just one or a few, you can use the for loop. The formula to follow is: for(DataType Initializer; EndOfRange; Increment) Do What You Want; In this formula, the for keyword, the parentheses, and the semi-colons are required. The DataType factor is used to specify how you will count the members of the array. The Initializer specifies how you would indicate the starting of the count. As seen in Lesson 8, this initialization could use an initialized int-based variable. The EndOfRange specifies how you would stop counting. If you are using an array, it should combine a conditional operation (<, <=, >, >=, or !=) with the number of members of the array minus 1. The Increment factor specifies how you would move from one index to the next. Here is an example: public class Exercise { public static void main(String[] args) { double numbers[] = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 }; for (int i = 0; i < 5; i++) System.out.println(numbers[i]); } } This would produce: 12.44 525.38 6.28 2448.32 632.04 In the above code, we knew the number of components in the array. Sometimes, this information will not be obvious. We saw already that the Array class is equipped with a field named length. You can use it in the second part of a for loop. Here is an example: public class Exercise { public static void main(String[] args) { double numbers[] = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 }; for (int i = 0; i < numbers.length; i++) System.out.println(numbers[i]); } } When using a for loop, you should pay attention to the number of items you use. If you use a number n less than the total number of members - 1, only the first n members of the array would be accessed. Here is an example: public class Exercise { public static void main(String[] args) { double numbers[] = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 }; for (int i = 0; i < 3; i++) System.out.printf("Number: %f\n", numbers[i]); } } This would produce: Number: 12.440000 Number: 525.380000 Number: 6.280000 On the other hand, if you use a number of items higher than the number of members minus one, the program would produce an error (we saw that the compiler would throw an exception).
In a for loop, you should know the number of members of the array. If you do not, the Java language allows you to let the compiler use its internal mechanism to get this count and use it to know where to stop counting. To assist you with this, you can use another version of the for loop. The formula to follow is: for (ArrayType Identifier: ArrayName) Use the Identifier The for keyword, the parentheses, and the colon are required. The first factor of this syntax, ArrayType is the same data type as the array (it can also be the name of a class as we will learn in when studying collections). The Identifier factor is a name of the variable you will use. This name is used to represent each component of the array. After the closing parenthesis, use the Identifier as you see fit. Like a for loop that accesses all members of the array, this other version is used to access each array member, one at a time. Here is an example: public class Exercise { public static void main(String[] args) { double numbers[] = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 }; for(double number: numbers) System.out.printf("Number: %f\n", number); } } This would produce: Number: 12.440000 Number: 525.380000 Number: 6.280000 Number: 2448.320000 Number: 632.040000
Because an array is a list of values, it can include items that are not useful in all scenarios. For example, having an array made of too many values, at one time you may want to isolate only the first n members of the array, or the last m members of the array, or a range of members from an index i to an index j. Another operation you may be interested to perform is to find out if this or that value exists in the array. One more interesting operation would be to find out what members or how many members of the array respond to this or that criterion. All these operations are useful and possible with different techniques.
Consider the following program: public class Exercise { public static void main(String[] args) { short[] numbers = new short[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 }; for (int i = 0; i < numbers.length; i++) System.out.println("Number: " + numbers[i]); } } This would produce: Number: 102 Number: 44 Number: 525 Number: 38 Number: 6 Number: 28 Number: 24481 Number: 327 Number: 632 Number: 104 Imagine you want to access only the first n members of the array. To do this, you can use an if conditional statement nested in a for loop. Here is an example that produces the first 4 values of the array: public class Exercise { public static void main(String[] args) { short[] numbers = new short[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 }; for (int i = 0; i < numbers.length; i++) if (i < 4) System.out.println("Number: " + numbers[i]); } } This would produce: Number: 102 Number: 44 Number: 525 Number: 38 You can use the same technique to get the last m members of the array. You can also use a similar technique to get one or a few values inside of the array, based on a condition of your choice. Here is an example that gets the values that are multiple of 5 from the array: public class Exercise { public static void main(String[] args) { short[] numbers = new short[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 }; for (int i = 0; i < numbers.length; i++) if (numbers[i] % 5 == 0) System.out.println("Number: " + numbers[i]); } } This would produce: Number: 525
|
|
|||||||||||||||||||||||||||||||
|