The Arrays in Java Programming Language is very important and it is necessary for a programmer to become the master of arrays programming.
Tutorials of Java arrays programming explaining the concept with examples
Arrays in the Java is one of the data structure for handling the similar types of objects. It is used to store the fixed size of objects in the array collection. For example you have to store the names of country in your Java Program you can easily create a fix size array object and the store it sequentially.
Programmers should know:
- What is Array?
- How to declare Array?
- How to add data (values) to an Array?
- Retrieving the value from array
So, let's see all these topics one by one.
What is Array?
Array is a simply a variable that can be used to hold many values, which can be accessed by array providing the position of the value in the array. You can think of array as a spread sheet which only one value at specified location. For example we can store the country in the following way:
In the array you can access the object with the help of position, for example you can to get the country 'US' then it can be accessed at position 1. In the array the positions starts from the 0 position.
How to declare array?
Now we will see how to declare an array? First of let me tell you that the array is fixed size and at the time of defining the array we have to specify the size of array.
Here is an example of declaring array to hold 5 data:
String[ ] countries; //Define variable
countries = new String[5]; //initialize array
or you can also define and initialize the array in one line as shown below:
String[ ] countries = new String[5]; //initialize array
Following is the way of declaring int type of array:
int[ ] selectedEmpIds = new int[5]; //initialize array
How to add data (values) to an Array?
To add the value we just have to specify the index and provide the value as shown below:
//Add values
countries[0]="India";
countries[1]="USA";
countries[2]="UK";
countries[3]="Germany";
countries[4]="Japan";
Retrieving the value from array
We can provide the index to get the value from array. Here is an example of getting the value at index 1:
System.out.println("Value at index 1: " + countries[1]);
So, this way you can use the array in Java programming.
Here is the complete example discussed:
import java.util.*; public class ArrayExamples { public static void main(String[] args) { System.out.println("Array Examples!"); String[ ] countries = new String[5]; //initialize country array //Add values countries[0]="India"; countries[1]="USA"; countries[2]="UK"; countries[3]="Germany"; countries[4]="Japan"; //Gettting the value at index 1 System.out.println("Value at index 1: " + countries[1]); } }
In the above example program we are declaring an array, then adding the values and finally retrieving the value at an index.
So, array is used in the Java Programming language to hold multiple values easily and it solves the problem of holding large number of data. It is very difficult to create one variable each for data and the ideal solution is to use the array or the Collection classes of the Java API.
[ 0 ] Comments