Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How Java Array List Helps in Organizing Same Type of Data

In this article we will see how the Java Array class assists in organizing the data of same type in a format that will be easily managed.

We know that program data is stored in the variables and usually takes the memory spaces randomly but in case when we need the data of the same type to store in the contiguous memory allocations, arrays are used.

We are blessed with Java providing an Array class which abstracts the array data-structure.

The java array helps the user to store the values of the same type in contiguous memory allocations for the reason that arrays are always a fixed length abstracted data structure and cannot be altered when required.

Introduction to Arrays Structure

It is widely known that Array is the most commonly used data structure in java that can contain multiple values of the same type. Also the length of an array cannot be increased or decreased.

Array usually comprise of the values which are implicitly referenced with the help of the index values. That is why in order to access the stored values in an array; the help of indexes are being taken. Assume we have an array that contains "n” integers, then we will have its first element being indexed with the "0" value and the last integer will be referenced by "n-1" indexed value.

Now assume an array that comprises of 12 elements with each element is holding a distinct value. We will have the first element being referenced by a[0] i.e. the first index value. We have filled the 12 distinct values in the array each referenced as:

a[0]=1
a[1]=2
...
a[n-1]=n
...
a[11]=12

Representing the Array Structure

Figure 1: Representing the Array Structure

How to declare an Array?

An Array variable is declared in much the same way as we declare a variable in java and array variable has a type and a valid Java identifier. This means that is has array's type (i.e. type of elements an array contain) and the name of the array. We make use of the [] notation to represent the variable as an array where these two brackets will be made use of to hold the array of a variable.

The array's name should follow the predefined conventions a illustrated below:

int[] array_name;   //declares an array of integers
String[] names;
int[][] matrix;  //this is an array of arrays

It is important to assign memory to an array when we declare it so as to set the size of the declared array. For example:

int[] array_name = new int[5];

The below code or example illustrates an array that comprises of 5 elements.

Listing 1: Representing an array with 5 elements

public class Array
{
 public static void main(String[] args)
 {
  int[] a = new int[5];
 }
}

Array Initialization

Once an array variable is declared, memory is allocated to it. We have the "new" operator that is being used for the allocation of memory to the array object. Below represents the way to use the "new" operator:

String names[];
names = new String[10];

Listing 2: Representing the simple example of an array

public class Sum 
{
  public static void main(String[] args) 
  {
  int[] x = new int [101];
  for (int i = 0; i<x.length; i++ )
  x[i] = i;
  int sum = 0;
  for(int i = 0; i<x.length; i++)
  sum += x[i];
  System.out.println(sum);
  }
}

Here, a variable 'x' is declared which has a type array of int, that is, int[]. The variablex is initialized to reference a newly created array object. The expression 'int[] = new int[50]' reflects the array to have 50 components. The field length is used in order to know the length of the Array.

Below represents the output for the given program:

C:\varun>javac Sum.java
C:\varun>java Sum
5050
C:\varun>

Introduction to Array Usage

The [] operator takes an "int" operand and then gives back the element at that index and we know that the array indices start with zero thereby the first element will be held by the 0 index. For Example :

  int month = months[4];  //get the 5th month (May)

In order to find the elements of interest in the program, it is necessary to runa loop through the array by the program, hence we have used "for" loop so as to examine each and every element in an array. For example :

String months[] = 
         {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
          "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
           //use the length attribute to get the number
          //of elements in an array
          for (int i = 0; i < months.length; i++ ) {
          System.out.println("month: " + month[i]);

An array of months is taken which is as follows:

     String months[] = 
    {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
     "July", "Aug", "Sep", "Oct", "Nov", "Dec"};

Let us run a “for” loop now so as to print each element individually starting from the month "Jan".

    for (int i = 0; i < months.length; i++ )

Introduction to Multi-dimensional arrays

We have looked at one-dimensional and two-dimensional arrays. In order to store data in more dimensions, we can use a multi-dimensional array which is nothing but is a collection of items which are accessed via n subscript expressions. For example, in a language that supports it, we have the element of the two-dimensional array x which is denoted by x[i,j].

The drawback point to note here is that the Multi dimensional arrays are not generally supported by the Java programming language however it supports an array of arrays. A two-dimensional array ' x' is an array of one-dimensional array, below reflects the example of the same.

     int[][] x = new int[3][5];

We have the expression x[i] that is made use of so as to select the one-dimensional array whereas on the other hand the expression x[i][j] is used to select the element from that array. As far as the first element of this array is concerned, it will be indexed with the "0" value whereas the last integer will be referenced by "length-1" indexed value.

Two-dimensional arrays

Two-dimensional arrays are basically defined as "an array of arrays" and for the reason that an array type is a first-class Java type; there can be an array of ints, an array of Strings, or an array of Objects. For instance, we have an array of ints that will have the type int[] and much on the similar lines we can have int[][] representing an "array of arrays of ints". Such an array is said to be a two-dimensional array.

Conclusion

We learnt how to make use of Java List Array to arrange the data of same type into a layout that can be managed with ease. Hope the article was helpful.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login