Arrays In Java
Arrays In Java
Arrays:It can be defined as the collection of similar elements together . Array Size Starts from "0" and ends at "n-1"
Example:
- Collection of student roll numbers comes under one single integer array.
- Collection of student names comes under one single String array.
- Collection of Faculty salarys comes under one single Float array.
Types Of Arrays:
There are three types of arrays they are :
- One/Single dimensional array
- Two Dimensional array
- Multi Dimensional array
One Dimensional array is used to collect the set of elements together . The Size of the array can be defined as "Product of number of bytes of datatype and size of the array."
Size of an array= number of bytes used by datatype * size of the array.
This type of arrays are not much needed to learn .
Syntax:
Initializing an array:
<datatype>[] variable={values};
Example for initializing :
int[] a={10,20,30,40};
Declaring an array:
<datatype>[] variable=new <datatype>[size];
Example for declaration:
int[] a=new int[3];
Program for 1-Dimensional Array By Passing Values at Compile time
class SingleDeimension
{
public static void main(String[] args)
{
int[] a={10,20,30,40};
System.out.println("values:");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
Save as:
SingleDeimension.java
Compile:
javac SingleDeimension.java
Run:
java SingleDeimension
Output:
values:
10
20
30
40
Note:
Length is used to return the length of an array . The memory can be allocated and de-allocated automatically in java .
Program for 1-Dimensional Array By Passing size at Compile time
import java.util.Scanner;
class SingleDeimensionDec
{
public static void main(String[] args)
{
int[] a=new int[5]; //Size of array has been initialized
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 values");
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
System.out.println("5 values are:");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
class SingleDeimensionDec
{
public static void main(String[] args)
{
int[] a=new int[5]; //Size of array has been initialized
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 values");
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
System.out.println("5 values are:");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
Save as:
SingleDeimensionDec.java
Compile:
javac SingleDeimensionDec.java
Run:
java SingleDeimensionDec
Input:
Enter 5 values:
10
20
30
40
50
Input:
Enter 5 values:
10
20
30
40
50
Output:
5 values:
10
20
30
40
Note:
From the above program we have declared the array variable passing the value of size at compile time .For every time of execution the size of array remains constant so to avoid this problem we have take the size of the array from the user at run Time .
Program for One Dimensional Array By Passing size at run time
import java.util.Scanner;
class SingleDeimensionDec1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int size; //Size Of Array is passed at Run Time
System.out.println("Enter size of array");
size=sc.nextInt();
int[] a=new int[size];
System.out.println("Enter"+"\t"+size+"\t"+"values");
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
System.out.println(size+"\t"+"values are:");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
class SingleDeimensionDec1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int size; //Size Of Array is passed at Run Time
System.out.println("Enter size of array");
size=sc.nextInt();
int[] a=new int[size];
System.out.println("Enter"+"\t"+size+"\t"+"values");
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
System.out.println(size+"\t"+"values are:");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
Save as:
SingleDeimensionDec1.java
Compile:
javac SingleDeimensionDec1.java
Run:
java SingleDeimensionDec1
Input:
Enter Size Value:
5
Enter 5 values:
10
20
30
40
50
Input:
Enter Size Value:
5
Enter 5 values:
10
20
30
40
50
Output:
5 values:
10
20
30
40
Note:
In this the size value is passed at run time . Depending on size values given , the memory is allocated .The size value is passed at Run Time . Can Differ from execution to execution .
2.Two Dimensional Array:
It is also the collection of similar elements in the form of table/matrix representation . The size of the array is product of bytes of datatype and Sum of sizes (Rows+columns).
It is also the collection of similar elements in the form of table/matrix representation . The size of the array is product of bytes of datatype and Sum of sizes (Rows+columns).
Size of two Dimensional Array = bytes of datatype * (Size of rows + Size of columns).
Syntax:
Initializing an array:
<datatype>[][] variable={{values of rows},{values of columns}};
Example for initializing :
int[][] a={{10,20,30,40},{1,2,3,4}};
Declaring an array:
<datatype>[][] variable=new <datatype>[size of rows][size of columns];
Example for declaration:
int[][] a=new int[3][3];
Program for 2-Dimensional Array By Passing values at run time
class DoubleDimensionInit
{
public static void main(String[] args)
{
int[][] a={{10,20},{30,40}}; //Values at Compile Time
System.out.println("values:");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.print(a[i][j]);
System.out.print("\t");
}
System.out.println("");
}
}
}
{
public static void main(String[] args)
{
int[][] a={{10,20},{30,40}}; //Values at Compile Time
System.out.println("values:");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.print(a[i][j]);
System.out.print("\t");
}
System.out.println("");
}
}
}
Save as:
DoubleDimensionInit.java
Compile:
javac DoubleDimensionInit.java
Run:
java DoubleDimensionInit
Output:
values:
10 20
30 40
Note:
From the above the size and the values are constant for every execution . since the values are passed at compile time.
Note:
From the above the size and the values are constant for every execution . since the values are passed at compile time.
Program for 2-Dimensional Array By Passing size at Compile time
import java.util.Scanner;
class DoubleDimensionDec
{
public static void main(String[] args)
{
int[][] a=new int[2][2]; //Size at Compile Time
System.out.println("Enter the values for 2*2 matrix :");
Scanner sc=new Scanner(System.in);
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("values:");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.print(a[i][j]);
System.out.print("\t");
}
System.out.println("");
}
}
}
class DoubleDimensionDec
{
public static void main(String[] args)
{
int[][] a=new int[2][2]; //Size at Compile Time
System.out.println("Enter the values for 2*2 matrix :");
Scanner sc=new Scanner(System.in);
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("values:");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.print(a[i][j]);
System.out.print("\t");
}
System.out.println("");
}
}
}
Save as:
DoubleDimensionInit.java
Compile:
javac DoubleDimensionInit.java
Run:
java DoubleDimensionInit
Input:
Enter the values for 2*2 matrix :
10
20
30
40
Output:
Enter the values for 2*2 matrix :
10
20
30
40
Output:
values:
10 20
30 40
Note:
Note:
From the above program all the values are passed at run time but the problem is the size of matrix will be constant . This can be resolved by passing the size of matrix and values of the matrix at Run Time.
Program for Two Dimensional Array By Passing size at run time
import java.util.Scanner;
class DoubleDimensionRun
{
public static void main(String[] args)
{
int rows;
int col;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values rows and columns:");
rows=sc.nextInt();
col=sc.nextInt();
int[][] a=new int[rows][col];
System.out.println("Enter the values for"+rows+"*"+col+"matrix :");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("values:");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.print(a[i][j]);
System.out.print("\t");
}
System.out.println("");
}
}
}
class DoubleDimensionRun
{
public static void main(String[] args)
{
int rows;
int col;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values rows and columns:");
rows=sc.nextInt();
col=sc.nextInt();
int[][] a=new int[rows][col];
System.out.println("Enter the values for"+rows+"*"+col+"matrix :");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("values:");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.print(a[i][j]);
System.out.print("\t");
}
System.out.println("");
}
}
}
Save as:
DoubleDimensionInit.java
Compile:
javac DoubleDimensionInit.java
Run:
java DoubleDimensionInit
Input:
Enter the values rows and columns:
2
2
Enter the values for 2*2 matrix :
10
20
30
40
Output:
Enter the values rows and columns:
2
2
Enter the values for 2*2 matrix :
10
20
30
40
Output:
values:
10 20
30 40
3.Multi Dimensional Array: This Type of arrays are generally not used. This are used to deal the objects related to space and when we require more than 2- Dimensions.This type of arrays are not much needed to learn .
Passing Array As a Parameter
import java.util.Scanner;
class ArrayAsPara
{
void arraySwap(int ar[])
{
System.out.println("Swapped Values:");
for(int i=ar.length-1;i>=0;i--)
{
System.out.println(ar[i]);
}
}
}
class ArrayAsParaTest
{
public static void main(String[] args)
{
int size;
System.out.println("Enter size of array:");
Scanner sc=new Scanner(System.in);
size=sc.nextInt();
int a[]=new int[size];
System.out.println("Enter values of array:");
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
ArrayAsPara aap=new ArrayAsPara();
aap.arraySwap(a);
}
}
Save as:
ArrayAsParaTest .java
Compile:
javac ArrayAsParaTest.java
Run:
java ArrayAsParaTest
Input:
Output:
Enter size of array:
2
Enter values of array:
10
20Output:
Swapped Values:
20
10
Note:
Array can also be passed as a parameter to a method to use the same array for any operation.
Command Line Arguments:This are the values passed to the program before running the program after compilation. User can provide the values to this arguments.In this each value is separated by space between the value.
Syntax:
java FileName value1 value2 ........
Example:
java CommandLineArguments void main (enter key).
Program Using Command Line Arguments
class CommandLineArguments
{
public static void main(String[] args)
{
int n=args.length;
System.out.println("Command Line Arguments");
for(int i=0;i<n;i++)
{
System.out.println(args[i]);
}
}
}
Save as:
CommandLineArguments .java
Compile:
javac CommandLineArguments .java
Run:
java CommandLineArguments VoidMainTechnologies Dilsuknagar
Note:
From the above program we can see that we have passed two values at run time . By giving the space between the values they are considered as the different values .
www.void Main Technologies.com
Comments
Post a Comment