0% found this document useful (0 votes)
53 views

Array Programs

The document contains Java programs that demonstrate the declaration, instantiation, initialization, and traversal of arrays. It covers one-dimensional, two-dimensional, and multidimensional arrays, including user input for array elements. Each program illustrates how to handle arrays in Java using loops for input and output.

Uploaded by

nayankokane62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Array Programs

The document contains Java programs that demonstrate the declaration, instantiation, initialization, and traversal of arrays. It covers one-dimensional, two-dimensional, and multidimensional arrays, including user input for array elements. Each program illustrates how to handle arrays in Java using loops for input and output.

Uploaded by

nayankokane62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

//Java Program to illustrate how to declare, instantiate, initialize and traverse the
Java array.
class ArrayDemo
{
public static void main(String args[])
{
int a[]=new int[5]; //declaration and instantiation
a[0]=100; //initialization
a[1]=200;
a[2]=300;
a[3]=400;
a[4]=500;
//traversing array
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}

2. //Java Program to illustrate one dimensional array

import java.util.*;
class OneDArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[]=new int[5];
System.out.println("Enter numbers for array:");
for(int i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}

3. //Java Program to illustrate two dimensional array


import java.util.*;
class TwoDArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][2];
System.out.println("Enter numbers for array:");
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(a[i][j]);
}
}
}
}

4. //Java Program to illustrate multidimensional array


import java.util.*;
class MultiDArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[][][]=new int[3][2][2];
System.out.println("Enter numbers for array:");
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
a[i][j][k]=sc.nextInt();
}
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
System.out.println(a[i][j][k]);
}
}
}
}
}

You might also like