Quick Reach
What is Java array?
An array is a type of variable that can store multiple values. An array is an object in Java that contains similar data type values.
Example of declaring and accessing array
How to declare an array
This is how a Java array can be declared:
ArrayDataType[] ArrayName;
OR
ArrayDataType ArrayName[];
Where:
- The ArrayDataType defines the data type of array element like int, double etc.
- ArrayName is the name of that array.
You can also create/ Instantiate an array by using the new keyword as follows:
int arrayName = new int[10];
Where [10] specifies that array length is ten or array can contain ten elements.
How to assign values to arrays
This is how you can assign a value to an array:
arrayName[0] = 10;
Alternatively, you can also assign values as follows:
int[]ArrList = {1, 2, 3, 4,5};
The above array is five elements length array.
Watch Video of this Tutorial or keep reading below
A few main points about arrays in Java:
- An array is a data structure in Java that can hold one or more values in a single variable.
- The array is a collection of similar type of values.
- Java has two types of arrays; single dimensional and multidimensional arrays.
- Java array index starts at 0.
String Array example
Why using Arrays
For beginners who have no idea about arrays may ask why we use arrays? Let us take an example to understand why arrays are utilized to store values.
Suppose we want to store 50 states in our Java program in variables. What we can do is to create 50 string variables and store states names in each variable, as below:
strState1, strState2, strState3, ….. , strState50;
where you can hold just one state name at a time. So you have to use 50 variables, e.g. strState1, strState2, strState3 and so on.
On the other hand, if you make that variable [state] an array, you can store all 50 state names in the single variable. Below we will show you in exercise how to create State array Java, initialize and then print array.
Example of Java int array
In this example, we will declare, initialize and access array items. The for loop is used (“foreach”) to display array items. The array type is int. See the example by clicking the image or link below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class array_ex {
public static void main(String []args) {
int arrex[] = {10,20,30}; //Declaring and initializing an array of three elements
for (int i=0;i<arrex.length;i++){
System.out.println(arrex[i]);
}
}
}
|
See graphic of above example
The output will be:
10
20
30
As you can see, an array is declared and initialized at first step.
You can also do this by following method where we will just declare and instantiate an array of 3 elements. Then assign values to each element of the array and finally printing the array elements by using a for loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class array_ex {
public static void main(String []args) {
int arrex[] = new int[3]; //declaring array of three items
arrex[0] =10;
arrex[1] =20;
arrex[2] =30;
//Printing array
for (int i=0;i<arrex.length;i++){
System.out.println(arrex[i]);
}
}
}
|
See graphic of above example
The output will be:
10
20
30
An example of a string array
The following example creates an array of the String elements to store and displays the State names. See this array example by clicking the link or image below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class array_ex {
public static void main(String []args) {
String strState[] = new String[3]; //declaring array of three items
strState[0] =“New York”;
strState[1] =“Texas”;
strState[2] =“Florida”;
//Printing array
for (int i=0;i<strState.length;i++){
System.out.println(strState[i]);
}
}
}
|
See graphic of above example
The output will be:
New York
Texas
Florida
An example of accessing specific array item
By using the index number of the array, you may access the specific array elements unlike in above example where we used the for loop for iterating through the complete arrays.
See this example, where I have used the same array i.e. US State names. After declaring and assigning three State names at 0, 1 and 2 index positions, the System.out.println is used to display the second array element.
As such, the array index starts at 0 position, so the second element means 1 index number:
See graphic of above example
1
2
3
4
5
6
7
8
9
10
|
public static void main(String []args) {
String strState[] = new String[3]; //declaring array of three items
strState[0] =“New York”;
strState[1] =“Texas”;
strState[2] =“Florida”;
//Printing array
System.out.println(“The State Name is: “ +strState[1]);
}
|
You can see, the index number is given inside the brackets for accessing specific array item.
Also see: Java String array | Java array length | Java copy array
Leave A Comment?