Java Array:
- The array is a collection of elements of the same type that are nearby memory spaces.
- We can store only a certain set of elements in a java array.
- The array index is based, the first element of the array is stored in the 0 index.
Types of Array:
- One Dimensional Array
- Two Dimensional Array
Java program contains steps in array:
- Announce the name of the array.
- Create an array.
- Start array values.
Array Features:
- It is always indexed. Index starts with 0.
- This is a collection of similar data types.
- It is in a closest memory place.
Array Pass with reference:
- Arrays are passed in terms of reference, or as an indicator of origin.
- This means that whatever array you make inside the function affects the root.
One dimensional array:
- A one-dimensional array is like a spreadsheet with data in only one line.
Syntax:
1 2 3 4 5 | dataType[] arr; (or) dataType []arr; (or) dataType arr[]; |
Explanation:
- An array declaration has two components: array type and array name.
- The type of an array is written as type [], where type elements are the type of data; Brackets are special symbols which indicate that this variable holds an array.
- The size of the array is not part of its type.
- With other types of variables, the announcement does not actually make a Java array; It only tells the compiler that in this variable there will be an array of the specified type.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package package_Java; class One_Array_Java { public static void main(String args[]) { // declaration and instantiation int a[] = new int[5]; // initialization a[0] = 10; a[1] = 20; a[2] = 70; a[3] = 40; a[4] = 50; // length is the property of array for (int i = 0; i < a.length; i++) System.out.println(a[i]); } } |
Output:
1 2 3 4 5 | 10 20 70 40 50 |
Another way of creating and initializing same one dimensional array is as shown below.
1 | int a[]={10,20,70,40,50}; |
Two Dimensional Array:
- In the Java software development language, two-dimensional array is like a spreadsheet with multiple rows and columns, each with different data in each slot.
- Each cell will be identified by its unique line and column index combination (e.g. SR 1 [3] [2]).
- We can use a two-dimensional array to store user IDs and passwords of different users.
Syntax:
1 2 3 4 5 6 7 | dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package package_Java; public class Two_Array_Java { public static void main(String[] args) { String str[][] = new String[3][2]; // 3 rows, 2 columns str[0][0] = "user_1"; str[1][0] = "user_2"; str[2][0] = "user_3"; str[0][1] = "pwd_1"; str[1][1] = "pwd_2"; str[2][1] = "pwd_3"; // This for loop will be total executed 3 times. for (int i = 0; i < str.length; i++) { // for loop wexecuted for 2 time every iteration. for (int j = 0; j < str[i].length; j++) { System.out.println(str[i][j]); } } } } |
Output:
1 2 3 4 5 6 | user_1 pwd_1 user_2 pwd_2 user_3 pwd_3 |