Categories
Java

Array Manipulation

Array is a finite collection of similar datatype arranged in a adjacent manner.

Array Syntax

Method – 1

datatype arrayname[]=new datatype[size];

 Method – 2

datatype arrayname[];
arrayname=new datatype[size];

Method – 3

datatype arrayname[]= {value1,vlaue2,…};

The following java program gets n numbers as inputs and prints the n numbers as output using array.

import java.io.*;
class ArrayExample
{
int n;
int a[];
BufferedReader br;
void get()throws Exception
{
System.out.println("Enter n");
br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
a=new int[n];
}
void pro()throws Exception
{
for(int i=0;i<n;i++)
{
System.out.println("Enter "+(i+1)+" element");
a[i]=Integer.parseInt(br.readLine());
}
}
void show()
{
for(int i=0;i<n;i++)
System.out.println(a[i]);
}
}
class ArrayExampleMain
{
public static void main(String arg[])throws Exception
{
ArrayExample obj=new ArrayExample();
obj.get();
obj.pro();
obj.show();
}
}

Leave a Reply

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