Arrays in java

We have been working with variables that hold only one value at a time. The integer variables we have set up have held only one number, and the string variables just one long string of text.

Think of a situation where we have to store ten numbers and find the sum of all the numbers. For the given problem we can declare ten variables but doing so will lead to an overhead of remembering names of all variables.

As well as all the variables will take random locations in the memory and traversing from one to another will lead to an inefficient result in terms of execution time.

So we need to have a way to store more than one value at a time in a single variable as well as data organization in such a manner in which traversal become faster. An array is a way to hold more than one value at a time..

Array is a collection of similar type of elements that have contiguous memory location. An array stores a sequence of values that are all of the same type.

The length of an array is established when the array is created. After creation, its length is fixed. Means that once the size of the array is defined it will occupy the space for each element whether we store that number of element or not.

That is why array is called as the static data structure. (An array is similar to a table of data, keyed by number. In Java an array is an object like all other objects)

Types of Arrays

  • One dimensional Array
  • Multi- dimensional Array

One dimensional Array :

A one-dimensional array is, essentially, a list of like-typed variables.

We want not just to store values but also to be able to quickly access each individual value. The method that we use to refer to individual values in an array is to number and then index them— if we have N values, we think of them as being numbered from 0 to N-1.

Note that starting index position on an array will always be 0. Array is represented as: arrays-java

Declaring Array :

Syntax for declaring array in java:


<Access Specifier><Data-Type><Name-of-Array> [] = new <Data-Type> [Size]; 

Here an array is being declared with the specified size. However, the above process can be broken down in two steps


 (i) <Access Specifier><Data-Type><Name-of-Array> []; 
(ii) <Name-of-Array>= new <Data-Type> [Size]; 


Here,

  • step (i) only declare an array but not specify size.
  • step (ii) specify the size of the array that how many elements can be stored in the array.


Example:


int numbers[]=new int[10]; 

The above example will declare an array named as numbers with the capability of storing 10 integer values. Each value is referred as an element of the array.

Assigning elements to the array :

We can assign elements to each index position in an array ranging from 0 to N-1, where N is the size of the array. In our case the index numbers will range from 0 to 9.


Syntax: 
Array-Name [index] = value; 
Example:
numbers[0]=15; numbers[1]=26; 

The above two statement will store 15 and 26 in the 0th and 1st index position respectively in the array numbers.

By default all the positions 0 to 9 have default value 0 stored.

Elements of an array can also be assigned at the same time when declaring the array. The format for doing so is as follows:


int numbers[]={2,6,9,4,8};
 or
 String names[]={"John", "Marry"?, "Peter", "Anderson"}; 

In the above example the size of the array is automatically decided by the number of elements passed.

Here in first case, the size of the numbers array would be 5 as the elements are 5 (2, 6, 9, 4 and 8). Whereas size of the names array would be 4 as the elements are 4 ("John?, "Marry?, "Peter?, "Anderson?).

Accessing element of array :

After storing elements in an array we may require to retrieve those elements at some point to time for calculations or in an expression for evaluation. This can be done again with the help of index number.

Let us consider an example, where we want to add the elements of an array numbers at index 0 and 1 and store it to a variable called sum:


Sum=numbers [0] +numbers [1]; 

Similarly when we have to print the element at index 3;


System.out.println(number[3]); 

Storing and accessing of elements of an array can be made easy and efficient with the help of loops.

For Loop example:

we have to store all even numbers between 1 and 15 in an array


class EvenNumbers {
 public static void main(String ar[]) {
  //declaring an array sized 10. Assuming there can be maximum 10 even numbers  
  int even_numbers[] = new int[10];
  int index = 0 //storing all even numbers between 1 and 15. 
  for (int i = 1; i & lt = 15; i++) {
   If(i % 2 == 0) {
    Even_numbers[index] = I;
    index++;
   }
  }
  //displaying all the elements of the array  
  for (int i = 0; i & ltindex; i++) {
   System.out.println(even_numbers[i]);
  }
 }
}

Arrays we learned so far were the one dimensional array means the elements are either stored in a row or a column. We have already learned about one dimensional array. Let us understand multi-dimensional array.

Multi-dimensional array

Think of a situation where you need to store roll number as well as marks of three subjects Math, English and Science for 10 students.

For this situation you can declare 4 array of int type. 1 for storing roll numbers and rest for three different subjects. But managing for different arrays will be cumbersome job.

How it would be if we can store all these values in a single array with 4 columns and 10 rows? It can be done through multi-dimensional array.

A multi-dimensional array can be called as an array of arrays. Syntax for declaring multi-dimensional array


<data-type><array-name>[][]=new <data-type>[rows][columns]; 
Example: 
int matrix[][]=new int [4][2]; 

The above code will declare a multi-dimensional array named matrix with 4 rows and 2 columns. Assigning elements in multi-dimensional array

In a multidimensional array, there are two indexes which we need to maintain one for rows (ranging between 0 to number of rows -1) and columns (ranging between 0 to number of rows -1).

For the above example, a memory for matrix would be allocated for 4 rows and 2 columns

multi-dimentional-array-java We can store values in in any allocated cell as follows:


matrix[0][0]=55; 
matrix[0][1]=15; 
matrix[1][0]=36; 
matrix[1][1]=25; 

After storing the elements in the matrix we can retrieve the elements stored in the same way we were accessing the one dimensional array.

a multidimensional array variable, specify each additional index using another set of square brackets. For example, the following declares a multi-dimensional array variable called multiD.


int multiD [][] = new int[4][5];

This allocates a 4 by 5 array and assigns it to multiD. Internally this matrix is implemented as an array of arrays of int.

The following program numbers each element in the array from left to right, top to bottom, and then displays these values:


// Demonstrate a multi-dimensional array. 
class multiDArray {
 public static void main(String args[]) {
  int multiD[][] = new int[4][5];
  int i, j, k = 0;
  for (i = 0; i & lt4; i++)
   for (j = 0; j & lt5; j++) {
    multiD[i][j] = k;
    k++;
   }
  for (i = 0; i & lt4; i++) {
   for (j = 0; j & lt5; j++)
    System.out.print(multiD[i][j] + "");
   System.out.println();
  }
 }
}


This program generates the following output:
0 	1 	2	 3 	 4 
5 	6 	7	 8	 9
10 	11	12 	 13  14
15 	16 	17	 18  19

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group