Array in Java Language
Array in Java language consist of
collection of elements that can be some variables or even values or we can say
that array consists of collection of elements all having same data type and
will be identified by a single name.
Why we need array?
In Java programming, sometimes we
need to store more than one values, we can store them using variables, but we
need to create seperate variable for every value. For example, if we want to
store 3 values, then we need to create 3 variables and store every value in
different variable.
int a,b,c;
a=10;
b=20;
c=30;
But when we want to store very
large values, for example 200 values, so for this case, making 200 variables
will be difficult task. For this situation, Java provides concept of Array.
Array can store fixed size collection of data and that data should be having
same data type.
How Array works
In order to store multiple values
using Array, we need to first declare an Array by proving it a name and data
type, and after that we can put values in this Array using its index, which
will represent individual values in Array.
It must be noted that Array has
contiguous locations inside memory in which we have addresses from start of
array to end of array.
How to create Array in Java
datatype arrayName[];
datatype[] arrayName;
datatype []arrayName;
For Example
class ArrayDemo
{
public
static void main(String[] ar)
{
int[]
a = {10,20,30};
System.out.println(a[1]);
}
}
In the above program, we have an
Array known as a having data type int and we are declaring it also initializing
it at the same time by providing values.
Now as we have 3 elements inside
array, so array index will also be 3, and they will start from 0.
a[0] = 10;
a[1] = 20;
a[2] = 30;
In the above example, we have used
System.out.println to print the value of Array at index 1, so we will get
answer 20
Example 2
Public class ArrayTest
{
Public
static void main(String ar[])
{
String[] Subjects = {"Java", "C++", "PHP", "Python"};
System.out.println(Subjects[0]);
}
}
This will print Java because its on
location number 0 in array Subjects
How to Access data inside Array
To access data inside Array, we
need its index number. When we place data in an Array, that data is indexed
starting from 0 which shows the first element inside array. For Example
int a[] = {1,2,3};
Here we have 3 items inside array
a. First array item is placed at location 0 which is the first index of array
a. Second item is placed at location 2 and that location has index 1 because
array index starts from 0.
To access an item of array, we have
to use its index number
System.out.println(a[0]);
Output will be 1
System.out.println(a[1]);
Output will be 2
System.out.println(a[2]);
Output will be 3
We can also replace or change the
array items. For Example
int a[] = {1,2,3};
and suppose we want to change second
location number to 22, so we have to do the following
int a[1] = 22;
System.out.println(a[1]);
Output will be 22
Finding the Length of Java Array
In Java language, we can find the
total number of items inside array using a property called length. For Example
We have an array a of int type
having some values in it.
int a[] = [1,2,3,4,5);
To find the length, we can use
System.out.println(a.length);
Output will be 5
Loops with Java Array
To print all the items inside
array, we can use loops in Java to get and print every item inside array. For
Example
int a[] = {1,2,3,4,5};
for
(int i=0; I < a.length; i++)
{
System.out.println(a[i]);
}
Output will be
1 2 3 4 5
Two dimensional Array
In Java, we can have array having 2
dimensions or more than two dimensions. This type of array is known as two or
multidimensional array.
In this type of array, we have more
arrays inside one array. For Example
int a[ ] [ ] = { {1,2,3},{4,5,6} }; // this is the 2
dimensional array a
System.out.println(a[1][1]); //
this will print 5
Output will be 5
Here, in above example, we have two
dimensional array a. This array has two further arrays. So to access this array
items, we need to provide two indexes, one for array, and second for array
element to access. Now to print 5, we need to provide index as a[1][1] because
this 5 belongs to second array and its index will be 1, and inside this array
value 5 also belongs to index 1.
To make every element of array to
print, we can use double for loop.
How to use nested for loop to print
multidimensional array items
Example
public class MultiArray
{
public
static void main(String[] ar)
{
int
a[][]={{1,2,3},{4,5,6} }; //2 dimensional array a
for(int
i = 0; i < a.length; i++) // this for loop will handle the array quantity,
in this case, its 2
{
for (int j=0; j
< a[i].length; j++) //this for loop will print array elements
{
System.out.println(a[i][j]);
// it will print all items inside multidimensional array a
}
}
}
}
Note:
Sometimes, we get error when
printing array values. This error belongs to array index and known as
ArrayIndexOutOfBoundException. This error can be explained with the help of
following example
Example
public class ArrayError
{
Public
static void main(String ar[])
{
int
a[] = {1,2,3,4};
for(int
I = 0; i<=4; i++)
{
System.out.println(a[i]);
}
}
}
Output will be
Exception in thread “main”
java.lang.ArrayIndexOutOfBoundsException: 4
1
2
3
4
This is because, we are printing
every array items and they start from 0 and will go up to 3 as we have total 4
elements inside array a and we have set the condition to < = , and because
of this = we are getting this exception.
Video Lectures

No comments:
Post a Comment