Arrays
A variable which can hold multiple values of similar data
type.
Can be of two types
1. Single
Dimensional
2. Array of
Array or Jagged Array
Single
Dimensional array
- Can have only one row and many columns.
Syntax
- datatype []arrayname=new datatype[size];
- datatype arrayname[]=new datatype[size];
- The size can also be user defined.
Example
WAP to ask the user how many numbers, he or she wants. Create
an array of that size. Input the data in that array and show the sum of all
those numbers.
import java.util.*;
public class ArrayTest1
{
public static void
main(String args[])
{
Scanner s=new
Scanner(System.in);
System.out.print("How many numbers : ");
int
size=s.nextInt();
int []ar=new
int[size];
System.out.printf("Enter %d numbers : ",size);
for(int
i=0;i<size;i++)
ar[i]=s.nextInt();
int sum=0;
for(int
i=0;i<size;i++)
sum=sum+ar[i];
System.out.println("Sum is "+sum);
}
}
Note:
Every array provides length
property to get size of array.
Example
WAP create an array having some values and show sum of all
those values.
public class ArrayTest2
{
public static void
main(String args[])
{
int []ar={6,7,5,4,4,6,6,4,4,4,8,9};
int sum=0;
for(int
i=0;i<ar.length;i++)
sum=sum+ar[i];
System.out.println("Sum is : "+sum);
}
}
Using
for-each loop
Another variant of for loop which works with arrays and
collections. It works automatically without knowing size of array and without
using array indexing.
Syntax
for(datatype
variable : arrayname)
{
Statements;
}
public
class ForEachTest
{
public static void main(String args[])
{
int []ar={6,7,5,4,4,6,6,4,4,4,8,9};
int sum=0;
for(int n : ar)
sum=sum+n;
System.out.println("Sum is :
"+sum);
}
}
Array of Array or Jagged Array
All Java
arrays are array of array or single dimensional arrays. Every row can have same
or different number of columns.
Syntax 1:
Equal number of columns in each row
int
[][]ar=new int[3][4];
Syntax 2:
un-equal number of columns in each row
int
[][]ar=new int[3][];
ar[0]=new
int[5];
ar[1]=new
int[7];
ar[2]=new
int[11];
Example
WAP to
create an array having 5 columns in first row, 7 columns in second row and 11
columns in third row. Input the data in the array and show that data.
import
java.util.Scanner;
public
class JaggedArrayTest
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int [][]ar=new int[3][];
ar[0]=new int[5];
ar[1]=new int[7];
ar[2]=new int[11];
for(int i=0;i<ar.length;i++)
{
for(int j=0;j<ar[i].length;j++)
{
System.out.printf("Enter data
in ar[%d][%d] : ",i,j);
ar[i][j]=s.nextInt();
}
}
for(int i=0;i<ar.length;i++)
{
for(int j=0;j<ar[i].length;j++)
System.out.printf("%4d", ar[i][j]);
System.out.println();
}
}
}
What is mean by triple dot (…)?
Called as
ellipses and used to indicate a dialog inside the menus and button and also
allows to create the method which can accept variable number of arguments.
Example
WAP having
a method which can take variable number of arguments and returns sum of all
those arguments.
public
class VarArgs
{
public static void sum(int... num)
{
int s=0;
for(int n: num)
s=s+n;
System.out.println("Sum is :
"+s);
}
public static void main(String... args)
{
sum(5,6,7);
sum(9,8,3,4,5,66,7,77);
}
}
-------------------------------------------------------------------------------------------
Hope you have learned something new from this post.
-------------------------------------------------------------------------------------------
Thank You
Keep coding :)
Keep learning :)
No comments:
Post a Comment