Pageviews

Monday 10 June 2013

Introduction to arrays in Java

by Unknown  |  in Object Oriented Programming at  Monday, June 10, 2013

Arrays are the most common data structure used in almost all high level programming languages.Arrays are a homogeneous collection of elements i.e. you can declare an array which consists of elements of similar data type only.For e.g. if we declare an array of data type int then we can use elements with int data type only.Array store elements in continuous memory locations.Arrays are of 2 types:


  • Single Dimensional arrays.
  • Multi Dimensional arrays.


Single Dimensional arrays:  In these types of arrays,we can use only single dimensional elements.


Multi Dimensional arrays:In these types of array two or more than two dimensional array can be declared.They are also known as matrix arrays,because they can be stored in the form of matrix.

In the following example we had declared a single dimensional array,which takes input of 5 elements and then displays the entered values.

WAP to show the array implementation in Java:

import java.util.Scanner;
class Array3
{
public static void main(String args[])
{
Scanner kp=new Scanner(System.in);
int play[]=new int[4];                                      //Array Declaration
System.out.println("Enter values of array");
for(int i=0;i<play.length;i++)
{
play[i]=kp.nextInt();                                          // Array Input
}
System.out.println("Entered elements are");
for(int i=0;i<play.length;i++)
{
System.out.print(play[i]);
}}
}


The output goes here:

Arrays

1 comment:

Proudly Powered by Blogger.