Declaring Arrays in Java

Hi guys, Here we discuss about one of the common and important topic is working with Arrays..means how to declare Arrays in Java.

An array is a collection of data storage locations, each of which holds the same type of data. Now, the question arises: why do we need this kind of a complex structure? There can be a situation where we need to store similar type of variables for a large number of data items.

simple types are fins as long as they are used for storing single data, but if the data to be stored is more complex,arrays are used.For example, to store names of 100 employees, we can declare 100 variables each storing a name in it, which is not a correct approach. Imagine writing 100 statements just to store the names of employees.

With arrays, we can declare a single variable that is capable of storing all the 100 employee names. the advantage of using arrays is that they simplify by replacing a number of statements with one or two statements.

In Java, arrays are created on dynamic memory that is allotted at  runtime by the JVM. Arrays are generally categorized as:

  • One-dimentional arrays
  • Two-dimentional arrays

One-dimentional Array

A one-dimentional array has only one index associated with it. It is a collection of data storage locations, each of which holds the same type of data. The number of indexes associated with any array is its dimention.Here we will discuss the following broad-level steps to use a one-dimentional array.

  • Creating a one-dimentional array
  • Intializing an array

Creating a One-dimentional Array

The following are the ways to carete a One-dimentional array:

One way of creating a one-dimentional array is by declaring the array first and then allocating the memory for it using a new operator. The following code declares a One-dimentional array:

  1. //declare books_category array of String type
  2. String books_category[];

Here, String determines the data type of each element of the array. The code specifies books_category as the name of a one-dimentional array variable.

Declaring an array does not ensure its successful creation. Once we have declared a one-dimentional array, the next step is to actually create that array by allocating memory to it.

  1. //allocating memory for storing 5 elements
  2. Sbooks_category = new String[5];

The above, memory is allocated to the books_category variable by usng the new operator. we can, however, combine the declaration and memory allocation statements into a single statement as well. see below line:

String Book_category[] = new String[5];

Here, the JVM allots memory for storing five String elements into the array.

Intializing an Array

The bellow code store elements into an array:

  1.  Books_category[0]=”Fiction”;
  2.  Books_category[1]=”autobiography”;
  3.  Books_category[2]=”children”;
  4.  Books_category[3]=”Travel”;
  5.  Books_category[4]=”Cookery”;

    The other way to declare a One-dimentional array and to directly store elements at the time of declaration is shown bellow

String Books_category[] = {“Fiction”,”Autobiography”, ‘Children”,”Travel”,”Cookery”};

we discussed  about declaring,allocating memory, and initializing an array:

  1. package com.vbbs;
  2. public class ArrayExample {
  3.    public static void main(String args[]) {
  4. int x;
  5. String Books_category[];
  6.  Books_category = new String[5];
  7. Books_category[0]=’Fiction”;
  8. Books_category[1]=”Autobiography”;
  9. Books_category[2]=”children”;
  10. Books_category[3]=”Travel”;
  11. Books_category[4]=”Cookery”;
  12. for(x=0;x<Books_category.length; x++) {
  13. System.out.println(Books_category[x]);
  14. }
  15. }
  16. }

Here Books_category is declared to group simple data types into a more complex structure  using a single index[].

Using a Two-dimentional Array

Till now, we have discussed one-dimentional arrays where only a single value can be allocated but there are cases when more values are needed to represent elements of an array. In this acse a two-dimentional array can be used to represent data.

The most commonly used array is the two-dimentional array can be used to represent data. class

  1. package com.vbbs;
  2. public class TwoD {
  3.      public static void main(String args[]) {
  4.     int arr[][] = new int[2][5];
  5.     int i,j;
  6.      int k=2;
  7.      for(i=0;i<2;i=+)
  8.      for(j=0;j<5;j++) {
  9.      arr[i][j]=k;
  10.         k=k+2;
  11. }
  12.      for(i=0; i<2; i++) {
  13.        for(j=0;j<5;j++)
  14.        System.out.println(arr[i][j]+” “);
  15. }
  16. }
  17. }

The code will displaying the first 10 even numbers using an int type two-dimwntional array called arr. This array type is declared with two rows and five columns.In multi-dimentional arrays, the left index specifies the row in the array, and the right index specifies the column.

Leave a Reply

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