Arrays
------
Array is a collection of similar type of values.
In one variable we can store many values using arrays.
static collection(fixed in size).
Arrays stores both primitives and objects.
Arrays are type safe.
Arrays are good at performance.
Types of arrays
---------------
one dimensional
Multi dimensional
one dimensional
---------------
Array with one subscript or one index is known as one dimensional array.
syntax
------
datatype varname[]=new datatype[size];
we can also declare array as below
datatype []varname=new datatype[size];
datatype[] varname=new datatype[size];
ex:
int a[]=new int[5];
Internal representation of arrays
---------------------------------
If the above statement is executed memory for array is allocated with 5 locations,the index of array always start with zero(0) and ends with size-1(4).
--
|a[0]
--
|a[1]
--
..
Programs
--------
import java.util.*;
class Test
{
public static void main(String args[])
{
int a[]=new int[5],sum=0;
int b[]=new int[5];
Scanner s=new Scanner(System.in);
System.out.println("Enter values into array");
for(int i=0;i<5;i++)
{
a[i]=s.nextInt();
}
for(int i=0;i<5;i++)
{
b[i]=a[i];
}
for(int i=0;i<5;i++)
{
System.out.println(b[i]);
}
}
}
Initializing Array using Array Initializer
------------------------------------------
Array initializer is used to initialize group of values with in parenthesis.
syntax
------
datatype varname[]={value1,value2,....};
Program
-------
import java.util.*;
class Test
{
public static void main(String args[])
{
int a[]={1,2,3,4,5,6,7};
for(int i=0;i<7;i++)
{
System.out.println(a[i]);
}
}
}
length variable
---------------
A length returns length of an array.
syntax
------
arrayvar.varname
ex:
Note:we cannot initialize size at the left the side of assigment operator in arrays;
which of the following array declarations are valid?
int a[]=new int[5];v
int a[]=new int[];iv
int a[5]=new int[5];iv
int a[5]=new int[];iv
int []a=new int[5];v
int[] a=new int[5];v
import java.util.*;
class Test
{
public static void main(String args[])
{
int a[]={4,2,3,4,5,6,4};
int count=0;
System.out.println("Enter key");
int key=new Scanner(System.in).nextInt();
for(int i=0;i<a.length;i++)
{
if(a[i]==key)
{
count++;
}
}
if(count>0)
System.out.println("element "+key+" is found "+count+" times ");
else
System.out.println("element not found");
}
}
2D Array
--------
Array with 2 subscripts is known as 2D Array.
2D Array is used to represent data in the form of rows and cols.
syntax
------
datatype varname[][]=new datatype[size1][size2];
size1-->rows
size2-->cols
ex:
int a[][]=new int[2][2];
internal representation
-----------------------
Program
-------
a.length.
3D Array
--------
Array with 3 subscript or index is known as 3D Array.
collection of 2d arrays.
syntax
------
datatype varname[][][]=new datatype[size1][size2][size3];
ex:
int a[][][]=new int[2][2][2];
size1-->no of 2d arrays
size2-->no of rows
size3-->no of cols
int a[]=new int[5];
If we try to insert/print values in an array we should not exceed the size of array,it leads to ArrayIndexOutOfBoundsException if we exceed the size.
Also we should not use negative index while working with arrays.
If we don't initialize an array it is automatically initialized with default values.
ex:
Program
-------
class Test
{
public static void main(String args[])
{
int a[]=new int[5];
for(int i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}
output
------
0
0
0
0
0
Arrays are obejects in java.we know that we create objects using new operator.
Arrays static collection i,e they are fixed in size
------
Array is a collection of similar type of values.
In one variable we can store many values using arrays.
static collection(fixed in size).
Arrays stores both primitives and objects.
Arrays are type safe.
Arrays are good at performance.
Types of arrays
---------------
one dimensional
Multi dimensional
one dimensional
---------------
Array with one subscript or one index is known as one dimensional array.
syntax
------
datatype varname[]=new datatype[size];
we can also declare array as below
datatype []varname=new datatype[size];
datatype[] varname=new datatype[size];
ex:
int a[]=new int[5];
Internal representation of arrays
---------------------------------
If the above statement is executed memory for array is allocated with 5 locations,the index of array always start with zero(0) and ends with size-1(4).
--
|a[0]
--
|a[1]
--
..
Programs
--------
import java.util.*;
class Test
{
public static void main(String args[])
{
int a[]=new int[5],sum=0;
int b[]=new int[5];
Scanner s=new Scanner(System.in);
System.out.println("Enter values into array");
for(int i=0;i<5;i++)
{
a[i]=s.nextInt();
}
for(int i=0;i<5;i++)
{
b[i]=a[i];
}
for(int i=0;i<5;i++)
{
System.out.println(b[i]);
}
}
}
Initializing Array using Array Initializer
------------------------------------------
Array initializer is used to initialize group of values with in parenthesis.
syntax
------
datatype varname[]={value1,value2,....};
Program
-------
import java.util.*;
class Test
{
public static void main(String args[])
{
int a[]={1,2,3,4,5,6,7};
for(int i=0;i<7;i++)
{
System.out.println(a[i]);
}
}
}
length variable
---------------
A length returns length of an array.
syntax
------
arrayvar.varname
ex:
Note:we cannot initialize size at the left the side of assigment operator in arrays;
which of the following array declarations are valid?
int a[]=new int[5];v
int a[]=new int[];iv
int a[5]=new int[5];iv
int a[5]=new int[];iv
int []a=new int[5];v
int[] a=new int[5];v
import java.util.*;
class Test
{
public static void main(String args[])
{
int a[]={4,2,3,4,5,6,4};
int count=0;
System.out.println("Enter key");
int key=new Scanner(System.in).nextInt();
for(int i=0;i<a.length;i++)
{
if(a[i]==key)
{
count++;
}
}
if(count>0)
System.out.println("element "+key+" is found "+count+" times ");
else
System.out.println("element not found");
}
}
2D Array
--------
Array with 2 subscripts is known as 2D Array.
2D Array is used to represent data in the form of rows and cols.
syntax
------
datatype varname[][]=new datatype[size1][size2];
size1-->rows
size2-->cols
ex:
int a[][]=new int[2][2];
internal representation
-----------------------
Program
-------
a.length.
3D Array
--------
Array with 3 subscript or index is known as 3D Array.
collection of 2d arrays.
syntax
------
datatype varname[][][]=new datatype[size1][size2][size3];
ex:
int a[][][]=new int[2][2][2];
size1-->no of 2d arrays
size2-->no of rows
size3-->no of cols
int a[]=new int[5];
If we try to insert/print values in an array we should not exceed the size of array,it leads to ArrayIndexOutOfBoundsException if we exceed the size.
Also we should not use negative index while working with arrays.
If we don't initialize an array it is automatically initialized with default values.
ex:
Program
-------
class Test
{
public static void main(String args[])
{
int a[]=new int[5];
for(int i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}
output
------
0
0
0
0
0
Arrays are obejects in java.we know that we create objects using new operator.
Arrays static collection i,e they are fixed in size
No comments:
Post a Comment