The content of this article below will revolve around the topic of Java - declaring and using arrays in Java, how to create arrays and manipulate them.
Java - Declaration and Utilization of Arrays in Java
Java provides a data structure called an array to store elements of the same data type. Arrays are used to store collections of data, but they are more commonly known for storing elements of the same data type.
Instead of declaring each variable one by one, such as number 0, number 1, ..., and number 99, you can declare an array variable like: number and then use numbers [0], number [1], ..., and number [99] to represent each variable individually.
Declaring Arrays in Java
To use an array in a program, you must first declare a variable to reference the array, and specify the type of array the variable can reference. Below is the syntax for declaring array variables in Java:
Syntax
Declare an array variable like: dataType[] arrayRefVar;
Or:
Alternatively: dataType arrayRefVar[];
Note:
The syntax dataType[] arrayRefVar is preferred. While the syntax dataType arrayRefVar[] originates from the C/C++ language and is accepted in Java.
Example
The following code snippets provide examples of the above syntaxes:
Declaration: double[] myList;
Alternatively:
Declaration: double myList[];
Creating Arrays in Java
In Java, you can create an array using the new operator with the following syntax:
Syntax:
arrayRefVar = new dataType[arraySize];
Above Syntax:
- Create an array using the new dataType[arraySize] syntax.
- Assign the reference of the newly created array to the variable arrayRefVar.
You can combine declaring an array variable, creating an array, and assigning the array reference in one statement, as shown below:
dataType[] arrayRefVar = new dataType[arraySize];
Alternatively, you can create arrays in Java using the following syntax:
dataType[] arrayRefVar = {value0, value1, ..., valuek};
Array elements are accessed via indices. Array indices start at 0, meaning they range from 0 to arrayRefVar.length-1.
Example
Below is the command to declare an array variable myList, create an array consisting of 10 elements of type double, and assign the reference to myList:
dataType[] arrayRefVar = new dataType[arraySize];
Alternatively, you can use the following command to create an array:
dataType[] arrayRefVar = {value0, value1, ..., valuek};
Array elements are accessed using indices. The array index starts at 0, meaning these indices range from 0 to arrayRefVar.length-1.
Below is an illustration of the myList array. Here, myList consists of 10 elements of type double with indices ranging from 0 to 9.
Handling Arrays in Java
When processing array elements, we often use for loops or foreach loops because all elements in the array have the same type and the size of the array is known.
Example
Here is a complete example of how to create, initialize, and manipulate arrays:
The example above returns the output result as:
Foreach Loop
In JDK 1.5, a new loop called the foreach loop or enhanced for loop was introduced, allowing you to iterate over the entire array seamlessly without using an index variable.
Example
The code below displays all elements in the myList array:
The example above returns the output result as:
Chapter 1.9
Chapter 2.9
Chapter 3.4
Chapter 3.5
Passing Arrays to Methods in Java
Similar to how you can pass primitive data type values to methods, you can also pass arrays to methods in Java.
For example, the method below displays the elements in an int array:
You can call it by passing an array. For instance, the command below calls the printArray method to display 3, 1, 2, 6, 4, and 2:
The java.util.Arrays class offers a plethora of static methods for sorting and searching arrays, comparing arrays, and filling elements into arrays.
Below is a table listing the methods for primitive data types:
In Java programming, understanding arrays opens up a vast realm of possibilities. Additionally, exploring other Java topics on Mytour such as java - loop control can further enrich your knowledge of this language.
