Tutorials On Defining, Processing And Implementing Arrays In Java
Array is the data structure which can store a fixed-size sequential collection of elements of the same type. An array can be used for storing collection of data but is often very much useful to take array as variables collection of the same type.
Declaring variables like number0, number1 and number99 is certainly not a desirable option. Instead it is better to declare one array variable such as numbers and then use it like numbers[0], numbers[1] and numbers [99] representing individual variables. Usually a specific element in an array is usually accessed by an index.
In most cases all arrays consist of contiguous locations in memory. The lowest address is usually the first element while the highest address is the last element.
How to Declare an Array
For declaring an array in a particular program, there must be declaration right to the reference of the array and one need to specify the type of array which the variable can refer. Here is the syntax which can declared an array variable-
Syntax dataType[] arrayRefVar; // preferred way. or dataType arrayRefVar[]; // works but not preferred way.
Note: The style dataType [] array RefVar is certainly the right way. The second style was adopted in Java for accommodating C/C++ programmers.
The examples of code snippets of this syntax are
double[] myList; // preferred way. or double myList[]; // works but not preferred way.
How To Create Arrays
One can create an array with use of new operator with the following syntax:
Syntax arrayRefVar = new dataType[arraySize];
With the above statement, there can be two things which can be done-
- It certainly creates an array with the use of new dataType[ arraySize].
- It also assigns the reference of various newly created arrays for variable arrayRefVar.
Well declaring an array as well as creating an array and assigning the reference of the array right to the variable can certainly be combined in one statement as given below-
dataType [] arrayRefVar = new dataType [arraySize];
There are alternative ways to create arrays as follows-
dataType[] arrayRefVar = {value0, value1, ..., valuek};
The array elements can be accessed through the index. Array indices are 0-based that is and they start from 0 to arrayRefVar.length-1.
Example
Here is another example where the following statement declares an array variable mylist creating an array of 10 elements of double type and assigns its reference right to myList-
double[] myList = new double[10];
Here is a pictorial reference of the array myList. Mylist holds ten double values as well as indices from 0 to 9.
Processing Arrays
While processing array elements, we use either for loop or foreach loop because different elements in an array are never of the same size and type is known.
Example
Here is the example of how to create, initialize as well as process arrays.
public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (int i = 0; i < myList.length; i++) { System.out.println(myList[i] + " "); } // Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is " + total); // Finding the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } System.out.println("Max is " + max); } }
This will give you results like
Output
1.9 2.9 3.4 3.5 Total is 11.7 Max is 3.5
The Foreach Loops
There is new way for the loop known as foreach loop or enhanced for loop, which can enable one to traverse the complete array sequentially for using index variable.
Example
The following code here displays all the elements in the array myList-
public class TestArray { public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5}; // Print all the array elements for (double element: myList) { System.out.println(element); } } }
This will produce the following result
Output
1.9 2.9 3.4 3.5
Passing Arrays to Methods
Just as one can pass the primitive type values right to methods, one can also pass arrays right to methods. Here are following method displaying elements in an int array-
Example
public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } }
One can invoke it by passing an array. The statement certainly invokes the printArray method for displaying 3, 1, 2, 6, 4 and 2-
Example
printArray(new int[]{3, 1, 2, 6, 4, 2});
How to return an array from the Method
There may be a method for returning an array. For example, there is method which returns an array which is the reversal of different array-
Example
public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; }
Here are certain whereabouts of Java Arrays:
Here are the following properties which are accepted by Java compiler.
- Arrays usually stores similar data types. Array can certainly hold data of data type’s values. This is one of the limitations of arrays as compared to other data structures.
- Each value stored which is usually stored in an array is certainly known as an element and various elements are indexed. The first element is usually added by default getting a 0 index. While the fifth element is added to get an index number of 4.
- Elements can be retrieved by certain index number.
- Elements of array can be stored in contiguous memory locations.
- Once array is usually created, the size is fixed. More elements cannot be added as arrays have limitation as compared to other data structures.
- Different array name can certainly represent different values. To store large quantity of data of same data types, array can be the easier option. Like for example, one array can store the salaries of 100 employees.
- Arrays are usually multidimensional
- Right at the time of creating an array, the size should be declared.
- As Java is concerned, arrays are predefined objects. Array elements cannot be manipulated.
This tutorial was aimed to give the readers an idea about Arrays in Java guiding them in the better way to master the programming in a errorless way.
Also Read,