Thursday 16 June 2016

Array in Java

In this blog post we will learn about how to use Array in java language.Before proceeding further we will learn what is a means of Array.

Array:

"An array is a data structure, which can store a collection of elements of the same(homogeneous)data type.An array is a container object that holds a fixed number of values of a homogeneous data type.The length of an array is established when the array is created or declared or assign.After creation, its length is fixed.Each item in an array is called an element,and each element is accessed by its index.The below figure shows clearly..."
















An array divided into two categories are given below








1-Singledimensional array

2-Multidimensional array


1-Declaring an array 
 Data Type[] variableName;
Here data type are int ,float,long,string and so on....

2-Declare the size /length of the array
 variableName=new Data Type[5];          // fixed array size for 5 elements

   3-Initialize all elements value 
There are two ways to declare value to array element
Approach First:

DataType[] variableName={};
OR
int[]a1={2,4,6};   //declares an array of integers

In the above case the length of the array is determined by the number of values provided between braces and separated by commas

Second Approach:

Alternatively, you can use the shortcut syntax to create and initialize an array:

int[] a1 = {
    10, 20, 30,
    40, 50, 60,
    70, 80, 90, 100
};
a1[0] = 100;  // initialize first element
a1[1] = 100;  // initialize second element
a1[2] = 100;
a1[3] = 100; so on...

Declares array  with other data type
Declaration of an array with other data types are given below ...

byte[] b1;
short[] s1;
long[] l1;
float[] f1;
double[] d1;
boolean[] bo1;
char[] ch;
String[] str;


Demo Example:
The following program, ArraySample, creates an array of integers, puts some values in the array, and prints each value to console output.

class ArraySample {
    public static void main(String[] args) {
     
        int[] a1;                  

        a1 = new int[5];
        a1[0] = 10;
        a1[1] = 20;
        a1[2] = 30;
        a1[3] = 40;
        a1[4] = 50;
     
        System.out.println("Element at index 0: "+ a1[0]);
        System.out.println("Element at index 1: "+ a1[1]);
        System.out.println("Element at index 2" + a1[2]);
        System.out.println("Element at index 3: "+ a1[3]);
        System.out.println("Element at index 4: " + a1[4]);
    }
}
The o/p of this program is:
10
20
30
40
50
                     


Multidimensional array:

In the Java programming language, a multidimensional array is an array whose components are themselves arrays. A consequence of this is that the rows are allowed to vary in length, as shown in the following MultiDemo program:

You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as int[][] number.Each element, therefore, must be accessed by a corresponding number of index values.



class MultiDemo {
    public static void main(String[] args) {
          int[][]number ={{1,2,3},{4,5}};
s
       System.out.println("The number: "+number[0][0]+"\n"+"The number at :"+number[0][1]);
       System.out.println("The number: "+number[1][1]+"\n"+"The number at :"+number[1][0]);

    }
}
The output from this program is:

The number: 1
The number at :2
The number: 5
The number at :4



Some most useful methods provided by the java.util.Arrays class are:

 1-(the binarySearch method):Searching an array for a specific value to get the index at which it is placed .
2-(the equals method):Comparing two arrays to determine if they are equal or not .
(the fill method):Filling an array to place a specific value at each index .
3-parallelSort method:Sorting an array into ascending order. This can be done either sequentially, using the sort method, or concurrently, using the parallelSort method introduced in Java SE 8. Parallel sorting of large arrays on multiprocessor systems is faster than sequential array sorting.Save time on processing .




1 comment: