Learn Java Array

Hey guys!! Today you will learn java array.

So, what are arrays and how we can declare and use arrays in java.

Learn Java Array: single dimensional array

An array is same as a variable but it can store more than one value at a time. But the only condition with array is you need to store rather only store same kind of values in an array.

Learn Java Array

For example, say you want to store 20 integers in an array, you can do it. But if you want to store 10 integers and 5 double values you can not do it. So you can only store same kind of data type in an array. So let’s see how we can declare an array in java,

Also Read – Array Copy Program in Java

public class Sample_Example {

public static void main(String[] args) {
 
int[] arr = {1,2,3,4,5};
 
int arr1[] = {1,2,3,4,5}; // not preferred
 
}

}

It is very simple, you just need to give the data type first of all for example you want to create an integer array then we give the data type and then we use these square brackets and then we give the variable name ‘arr’ and then you can initialize this array in these flower brackets or curly braces and whatever comes inside these curly braces for example 1,2,3,4,5 these are the values or elements of the array. So it can store multiple integer so we have stored right now five integers in this array.

There is another way of declaring array and which is like in the example above. Here you just use your data type and then ‘arr1’ for example and then you give square bracket and then you can give values to the array and this will also work fine. But it’s not the preferred way of declaring array. So, first way of declaring array is preferred. But it will work if you want to declare it.

Now there are different ways of declaring array and the most basic one is mentioned above in example. So, the different ways are,

public class Sample_Example {

public static void main(String[] args) {
 
int[] arr = {1,2,3,4,5};
 
int[] arr = new int[5];
 
int[] arr = {1,2,3};
 
int[] arr = new int[]{1,2,3};

}

}

These are the most used ways of declaring an array. So first is mentioned above. Second you can declare an array like integer square bracket and the name and is equal to new. Here we have not learned what is new keyword. So i will not go deep into this and as well as the fourth declaration in the above example. We will learn when we will learn how to use classes and objects. So we will use third declaration array example, as above. But i wanted to show you these declarations are also possible.

Now if you declare an array these are called elements of the array, that is, after = sign,

int[] arr = {1,2,3,4,5};

And right now our array contains 5 elements and the position at which these values are stored are called index and the index of the array starts from zero. So for example i want to print the value 2. So i can write,

public class Sample_Example {

public static void main(String[] args) {
 
int[] arr = {1,2,3,4,5};
 
System.out.println(arr[1]);

}

}

So the actual position of this value is 2 is 1. And whenever you write index 1 it will give you value 2. So in this way you can access the value of array.

In case if you want to print all the values in the array we use while loop to print all the values in the array like this,

public class Sample_Example {

public static void main(String[] args) {
 
int[] arr = {1,2,3,4,5};
 
int a = 0;
 
while(a < 5)
{
System.out.println(arr[a]);
a++;
}

}

}

Output:

1
2
3
4
5


Learn Java Array: Multidimensional Array

The syntax for multidimensional array is

Also Read – Two Dimensional Array in Java

public class Sample_Example {

public static void main(String[] args) {
 
int multi_Array[][] = {{1,2,3,4},{5,6,7,8}};

}

}

Here multi_Array is just name of the array and instead of just one square bracket you actually add two. The first one is for the row and the second one is for the column. This is of course is a multidimensional array initializer and this is the easiest way to create multidimensional arrays. So you pretty much have your main array right here. Once that, just fill it with number 1,2,3,4 and then anytime you want to go to a new row just press comma and new curly brackets and put 5,6,7,8. What this would mean right here is you have an array with your first row being 1,2,3,4 in your second row being 5,6,7,8. So whenever you want to call an element in your array the first row of this, that is, the first call is 0. That is,

multi_Array[0][1] = 1
multi_Array[0][2] = 2
multi_Array[0][3] = 3
multi_Array[0][4] = 4

The second row will be

multi_Array[1][0] = 5
multi_Array[1][1] = 6
multi_Array[1][2] = 7
multi_Array[1][3] = 8

So that’s how you call elements in a multidimensional array.

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *