Array Concept Simplified in Java

0
64
Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

Array Concept in Java
Array Concept in Java
Array Concept in Java

Array Concept in Java:

The first implementation to store values in any programming language is variable, like
int a=10;
int b=20;
so if we need to store 10 values, We need to create 10 different variables.
Simple!!

What is the problem here?

  • If we want to store more data, we need to use more variables. Imagine a scenario where we are trying to capture different marks obtained by class -II students in different subjects. Assume this particular school has 4 different subjects for Class-II and it is having 30 students. So!!! we need to create 4*30=120 variables!!!
  • Think about readability!!! may be there will minimum 120 lines to declare, initialize and assign values to these variables. Readability will be worst for these cases.
  • No programmatic control over these variables. Let us say we want to fetch values one by one and display, for this case minimum we need to write again 120 lines of code. The more advanced concept of the Java language like loop cannot be applied on this.

So, What could be the solution?? we want to reduce the variable count, Java has come up with a concept called Array.

Array in Java

An array is a very common data structure.It is a group of contiguous or related data items that share a common same name. It is the most efficient way to store and access objects(object handles).

It is a collection of type-indexed, homogeneous data elements[All the elements must be the same datatype i.e an array holds multiple values of the same type(primitive type or object reference or instances of the same class type)].

So number of data items are collected together into a single unit and provided a name is called an Array.in more generic words, Array is a sequence of items referred by a name, The items in an Array are numbered(index).

Individual items are referred by the position number. Array is a linear sequence for storing objects.In an array, a sequence of objectselements packaged together under one single identifier name.The speed is very fast while accessing an Array but the size is fixed.
Once defined Array is of fixed size and can not be increased to accommodate more elements. It starts with index zero(0). An array is defined and used with a square bracket called indexing operator-[].

We can find out the length of an Array but can not tell how many elements are there in the Array.A particular value is indicated by writing a number called index number or subscript in bracket after the array name.

The definition of Array is a numbered Sequence of items which are all of the same type. The total number of items in an array is called length of the Array. The position number of an item in an Array is called the index of that item. The type of the individual items in an Array is called the base type of the Array.(Base type can be primitive type or class name or interface name)

If base type of an Array is String,It is referred as an Array of String Similarly for int, it is referred as an Array of ints. The Array can be visualized as a container not values.It can hold a value of a specific type(base type) which can be changed. Values are stored in an Array.Each individual variables that make the Array is called the Elements of the Array.
Java supports C and C++ style array declaration like int myArray[]; and also support Java-style array declaration int[] myArray;
In this way an object is also a data structure of named instance variable.Array can hold Primitive Data types and Objects(set of buttons,text boxes).

Please refer Array class for details on Array.

Create an Array

Creation of Array involves 3 major steps: They are as follows:

  • declare the array.we do not enter size of the array here.
  • create memory location using new operator.
  • assign values in the memory locations

We can declare an array in the following manners:

  1. (Element type)[] (Array name)=new (element type)[size]
  2. (Element type) (Array name)[]=new (element type)[size]
  3. (Element type)[] (Array name)={element1,element2…..elementN}

Where (Element type) is the type or identifier of the array.size is the number of the element specified to be reserved for the array in the memory.

This size attribute is similar to a public final variable of an array. Once it is specified or defined, we can not change it.The bracket [] operator is to recall the syntax for referring individual items in an Array.
Example –


int myArray[]=new int[7];

int myArray[] does not create an array, It is just a declaration. Once declared, the compiler has no idea about the size of the Array. At this point what we have, is the handle of the Array that refers to the Array.

Also there is no constructor to call and argument list is omitted for new keyword. The elements created in this way are initialized to default values for their type.

The reference variable will have no value as no space has been allocated to the Array. To create storage space for the array we must initialize it. Array gets created when we use the new operator along with the size of that Array.new returns a reference to the myArray variable.
Once we create an array with the new operator, all elements of the array are initialized with default values until actual values are placed in array position. Any elements of the Array can hold a value of null meaning it is not pointing to anything.Java Array variables can not hold the Array rather they can only point or refer to an Array.
As Arrays are created by using new operator, they are objects. Being an Object they belong to a class(Object the super class of all classes). The elements of the array are essentially instance variable of the Array Object.

In the above example:
If an Array is referred by myArray then element at index 0 is referred by myArray[0],index 1 is referred by myArray[1] and index-N in myArray is referred by myArray[N] .

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here