unit-1javaArrays
unit-1javaArrays
By Gargi mukherjee
ARRAYS
An array is a collection of similar type of elements which
has contiguous memory location
Java array is an object which contains elements of a
similar data type. Additionally, The elements of an array
are stored in a contiguous memory location. It is a data
structure where we store similar elements. We can store
only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the
array is stored at the 0th index, 2nd element is stored on
1st index and so on.
WHAT ARE ARRAYS?
int[ ] marks={72,61,81,79,72};
In JAVA, int is of 4 bytes, total space=4*5=20 bytes
GRAPHICAL REPRESENTATION
Index
marks
72 61 81 79 72
value
TYPES OF ARRAY IN JAVA
There are two types of array.
• Single Dimensional Array
• Multidimensional Array
• Jagged Arrays
even
0 1 2 3 4
7 74 66 68 70
2
value
DEMONSTRATION
int[] marks = new int[20];
marks[0] = 2;
marks[1] = 3;
int[] marks2=marks;
System.out.println(marks2[0]);
marks2[0]=5;
System.out.println(marks[0]);
OUTPUT
2
5
ARRAY LENGTH
Refer to array length using length
A data member of array object
array_variable_name.length
for(int k=0; k<marks.length;k++)
….
Sample Code:
int[ ] marks = new int[5];
System.out.println(marks.length);
Output: 5
EXAMPLE OF ARRAY
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
FOR-EACH LOOP FOR JAVA ARRAY
1. for(data_type variable:array){
3. }
4. class Testarray1{
6. int arr[]={33,3,4,5};
8. for(int i:arr)
9. System.out.println(i);
10.}}
JAVA PROGRAM TO DEMONSTRATE THE WAY OF
PASSING AN ARRAY
class Testarray2{
//creating a method which receives an array as a parameter
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i])
min=arr[i];
System.out.println(min);
}
A[2][0] A[2][1] 14
INITIALIZING ARRAY OF ARRAYS
int[][] array2D = { {99, 42, 74, 83,
100}, {90, 91, 72, 88, 95}, {88, 61,
74, 89, 96}, {61, 89, 82, 98, 93},
{93, 73, 75, 78, 99}, {50, 65, 92, 87,
94}, {43, 98, 78, 56, 99} };
//7 arrays with 5 elements each
class twoarray{
public static void main(String args[]){
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
OUTPUT
123
245
445
ARRAYS OF ARRAYS OF VARYING LENGTH
(JAGGED ARRAYS)
All arrays do not have to be of the same length
float[ ][ ] samples;
samples=new float[5][];//defines no of rows in an
array
samples[0]=new float[6];
samples[1]=new float[101];
Not required to define all arrays
f we are creating odd number of columns in a 2D array,
it is known as a jagged array. In other words, it is an
array of arrays with different number of columns.
INITIALIZING VARYING SIZE ARRAYS
int[][] uneven = { { 1, 9, 4 }, { 0,
2}, { 0, 1, 2, 3, 4 } };
//Three arrays
//First array has 3 elements
//Second array has 2 elements
//Third array has 5 elements
To allocate memory for a 2D array, we need to specify the
memory for the first(Leftmost) dimension. Then remaining
dimensions can be allocated separately.
For eg:
int arr2d[][]=new int[3][];
arr2d[0]=new int[3];
arr2d[1]=new int[3];
arr2d[2]=new int[3];
v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity: " +v.capacity());
v.addElement(new Float(9.4));
v.addElement(new Integer(10));
System.out.println("Current capacity: " +v.capacity());
v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("First element: " +(Integer)v.firstElement());
System.out.println("Last element: " +(Integer)v.lastElement());
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");
// enumerate the elements in the vector.
Enumeration vEnum = v.elements();
System.out.println("\nElements in vector:");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
}
}
The output from this program is shown here:
Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.
Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12