ARRAYS


Array is a homogeneous data structure that store collection of same type of data elements under a single variable identifier referenced with index numbers in the memory.

Array always start with the index zero.

TYPES OF ARRAY

(a) One Dimensional Array

-> One dimensional arrays are  lists of information of the same data type and their elements are stored in contiguous memory location in their index order.

A[m] : m number of rows or columns

int array[] = new int[5];

array[0] array[1] array[2] array[3] array[4]



(b) Two Dimensional Array

-> It is an array in which each element is itself an array. It is also known as Matrix.

A[m][n] : - has m number of rows and n number of columns with m x n elements.

 array[0][0] array[0][1]  array[0][2]
 array[1][0]   array[1][1] array[1][2] 
 array[2][0]  array[2][1]  array[2][2]



(c) Multi-Dimensional array

-> It is an arrays of arrays.They use more than one index to access array elements.

datatype arrayName [a] [b] [c] [d] ............ [z] ;

 

 

RAGGED ARRAYS

Ragged arrays are arrays in which different rows have different lengths. It means different columns and rows will have different number of elements. It is the case that arises when an array is multidimensional and we don't want to have all elements or sub arrays inside our array of same length. It allows us to have array with varied length.

1

1  2

1  2  3

1  2  3  4

 

HOW TO DECLARE AND CREATE ARRAY

Specify the datatype for elements of array followed by [ ] square brackets then the array variable name.

int[] arr;

Above Statement only declares the array, it does not initialize the array variable arr with actual array.

We use the new operator to create the array.

new operator allocate memory at run-time.Java initializes the array of type int values to 0 , double to 0.0 , float to 0.0 and char to '\0' .

int[] arr = new int[10];

We can declare array in following ways too :

int[] a; 

or

int a[];

or

int []a;

INITIALISING THE ARRAY

We can initialise the array at the time of declaration, by putting the values in the braces separated by commas.

SINGLE DIMENSIONAL ARRAY

int array[] = {1 , 2 ,3 , 4, 6};

MATRIX

int arr[][] = { 

{1 , 2 , 3 } , {2 , 3 , 4} , {5 , 6 , 7}

}

 

class Solution
{
   public static void main(String[] args)
   {
       int i[] = new int[5];
       float f[] = new float[5];
       double d[] = new double[5];
       char c[] = new char[5];
       
       System.out.println("int starting value is = " + i[1]);
       System.out.println("float starting value is = " + f[3]);
       System.out.println("double starting value is = " + d[2]);
       
       if( c[1] == '\0')
       {System.out.println("char starting value is = null character"); }
   }
}

Output

int starting value is = 0
float starting value is = 0.0
double starting value is = 0.0
char starting value is = null character

PROGRAM


WAP in Java to create a multi-dimensional array of 4x4 matrix in which you have to find out the sum of hourglasses and print out the maximum sum among all the hourglasses.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
public class Solution
{
    Scanner sc = new Scanner(System.in);
    int arr[][] = new int[4][4]; 
    int sum , maxsum=0;
    public void input()
    {
        System.out.println("enter matrix elements");
        for(int i = 0 ; i < 4 ; i++)
        {
            for(int j = 0 ; j<4 ; j++)
            {
                arr[i][j] = sc.nextInt();
            }
        }
        for(int i = 0 ; i < 4 ; i++)
        {
            for(int j = 0 ; j<4 ; j++)
            {
                System.out.printf("%d \t", arr[i][j] );
            }
            System.out.print("\n");
        }
        
    

    for(int i = 0 ; i <=1 ; i++)
    {
        for(int j = 0 ; j<=1 ; j++)
        {
            sum = arr[i][j] +
                    arr[i][j+1] +
                    arr[i][j+2] +
                    arr[i+1][j+1] +
                    arr[i+2][j] +
                    arr[i+2][j+1] +
                    arr[i+2][j+2] ;
            if(sum>maxsum)
            {
                maxsum=sum;
            }
        }
        
    }
    System.out.println("maximum sum in hourglasses is : " + maxsum );
}

   public static void main(String[] args)
   {
       Solution s = new Solution();
       s.input();
   }
}

OUTPUT (we have given the input and you can see what we have given in the output.)

1  2   3   4   
3   4   5   6   
4   5   6   7   
0   0   1   1   
maximum sum in hourglasses is : 32


 

ARRAYS CLASS

There is a class of arrays with name -> Arrays.

To include class Arrays you have to import package -> java.util.Arrays

There are many inbuilt methods that you can use like

->linear search

->binary search

->bubble sort

->binary sort etc..



ArrayList

In many programming languages like C and C++ , there was a problem to set the size of array before using it. It creates a problem cause we always not know how many elements we will require beforehand. Eg: in a company of 150 employees there could be addition of one new member or one employee leaves the company , it all creates a headache to make array again.

To solve this problem Java introduced a new class called ArrayList.

ArrayList class is similar to array , but it automatically adjusts its capacity as you add and remove elements i.e. ArrayList is a class that can grow or shrink at the runtime.

Note : arrays of this class must hold objects , not just simple datatypes.