Tutorials

Arrays


Array operations


We can perform operations on arrays like store, traverse, search retrieve, delete, etc

But before that we need to create the array, initialize and declare its type. 

Initialization will specify the size of the array and will also define the type of data it will hold.

 

Different programming language initialize and declare array in different way. But thing to remember is core concept of array remains same.

We will use both C and Java for demonstration purpose.

 

Arrays in java

Declaration

Declare array

Example

dataType  arrayName[ ];

int myArray[];

dataType[ ]  arrayName;

char[] charArray;

Instantiation

The array that we declared above now can be instantiated as below:

 

Instantiation

Example

arrayName[ ] = new dataType[size];

myArray = new int[5];

 

charArray = new char[3];

Once we have declared and instantiated the array we need to initialize the values to it.

 

Initialization Example

 

//myArray is of size 5

myArray[0]=2;

myArray[1]=12;

myArray[2]=233;

myArray[3]=1;

myArray[4]=21;

//charArray is of size 3

myArray[0]='s';

myArray[1]='a';

myArray[2]='l';

 

We can declare, instantiate and initialize the array in a single step as shown below:

int myArray[] = {2, 12, 233, 1, 21};

char charArray[] = {'s', 'a', 'l'};

 

Operations

The array that we create can perform many operations. 

We will study some of the commonly used operations on array:

  • Insert → New elements can be inserted at any index in array.
  • Delete → Existing elements can be deleted at any index in array.
  • Traverse → All the elements of the array can be traversed.
  • Search → We can search for particular index and get its index.
  • Update → We can update an element at any index.

We will see each of the operations on Array in upcoming chapters.



Please Share this page
Views : 69
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!