Java Arrays
Java Arrays are data structures which store a fixed number of values of same data type. An array is used to store data of same data type in a sequence and is easy to access and modify.
Instead of defining every variable individually of the same type we create an array and simply add all values in it. Array saves values in different indexes and they can access using these indexes for later use.
Initializing or declaration of array in Java:
Syntax: data_type [ ] variable_name;
Example: int [ ] a;
In java there are so many ways of creating arrays and assign values to it some are following.
int [ ] arr;
int arr[ ];
int arr[ ] = new int[ array_length ];
int [ ] array = {value1, value2 … };

Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.*; public class MyFirstProject{ public static void main(String args[]){ int[] arr = {11,22,33,44,55}; System.out.println("Printing Array Values"); System.out.println("Value in Index 0 = "+arr[0] ); System.out.println("Value in Index 1 = "+arr[1] ); System.out.println("Value in Index 2 = "+arr[2] ); System.out.println("Value in Index 3 = "+arr[3] ); System.out.println("Value in Index 4 = "+arr[4] ); } } |
Looping Array:
As we see in above example to print every value of array we have to write separate condition for every index of an array so to reduce this problem loops are used like for loop, while loop and do while also.
Example using for loop:
1 2 3 4 5 6 7 8 9 10 |
import java.util.*; public class MyFirstProject{ public static void main(String args[]){ int[] arr = {11,22,33,44,55}; System.out.println("Printing Array Values"); for(int i=0;i<arr.length;i++) System.out.println("Value in Index "+i+" = "+arr[i] ); } } |
Example using while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.*; public class MyFirstProject{ public static void main(String args[]){ int[] arr = {11,22,33,44,55}; System.out.println("Printing Array Values"); int i=0; while(i<arr.length){ i++; System.out.println("Value in Index "+i+" = "+arr[i] ); } } } |
foreach loop in Java:
In java for access array all values in one go a special loop is used that’s called foreach loop.
Syntax:
for( data_type_same_to_array_data_type variable_name : array_name){
}
Example:
1 2 3 4 5 6 7 8 9 |
import java.util.*; public class MyFirstProject{ public static void main(String args[]){ int[] arr = {11,22,33,44,55}; System.out.println("Printing Array Values"); for(int a : arr) System.out.println("Value in array = "+a ); } } |
Array as Parameters/argument in functions:
We can send array as a parameter in any function and this function also receive it as an argument and function in java also return an array.
Array as Parameter:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.*; public class MyFirstProject{ public static void aryfun(int[] arr) { for (int x = 0; x < arr.length; x++) { System.out.print(arr[x] + " "); } } public static void main(String args[]){ int[] arr = {11,22,33,44,55}; aryfun(arr); } } |
Array as return type of function:
To return an array the function must be having a return data type of array. It mean if a function return integer array the function data type must be int[ ] a();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; public class MyFirstProject{ public static int[] swap(int[] arr) { int[] result = new int[arr.length]; for (int i = 0, j = result.length - 1; i < arr.length; i++, j--) { result[j] = arr[i]; } return result; } public static void main(String args[]){ int[] arr = {11,22,33,44,55}; swap(arr); } } |
2D array in Java:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.*; public class MyFirstProject{ public static void main(String args[]){ int[][] arr = {{11,22},{33,44},{55,66}}; for(int i=0;i<2;i++){ for(int j=0;j<3;j++){ System.out.println(arr[j][i]); } } } } |

In the next tutorial, you will learn to create and use ArrayList in Java.