Lecture - 4 Arrays in Java
Lecture - 4 Arrays in Java
ARRAYS:
• An array is a collection of similar type of elements which has a
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.
• 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.
There are two types of arrays.
• Single Dimensional Array
• Multidimensional Array
SINGLE DIMENSIONAL ARRAY
Single Dimensional Array in Java
Declaration:
Syntax : dataType[] arr; (or) dataType []arr; (or) dataType arr[];
Ex: int[] arr; or int []arr; int arr[];
Instantiation:
Syntax: arrayRefVar=new datatype[size];
Ex: arr = new int[10];
Declaration and Instantiation :
Syntax: datatype arrayRefVar[] = new datatype[size];
Ex: int arr[] = new int[10];
Single Dimensional Array in Java
Initialization:
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
Or
int a[]={10,20,70,40,50}; //declaration, instantiation and initialization
Single Dimensional Array in Java
Example :
class program1
{
Output:
public static void main(String args[]) 10
20
{ 70
40
//declaration, instantiation and initialization
50
int a[]={10,20,70,40,50};
//printing array
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
Single Dimensional Array in Java
For-each Loop for Java Array
class arrcopy2
{
• Here, we can see that we have
public static void main(String[] args)
changed one value of
{
the numbers array. When we print
int [] numbers = {1, 2, 3, 4, 5, 6};
the positiveNumbers array, we can
int [] positiveNumbers = numbers; // copying arrays
see that the same value is also
changed.
numbers[0] = -1;
• It's because both arrays refer to the
for (int number: positiveNumbers)
same array object. This is because of
{
the shallow copy.
System.out.print(number + ", ");
}
• Now, to make new array objects while
}
copying the arrays, we need deep
}
copy rather than a shallow copy.
Output:
-1,2,3,4,5,6
Single Dimensional Array in Java
2. Copy Arrays Using Looping Construct (Deep Copy)
class arrcopy3
{
public static void main(String[] args)
{
int [] source = {1, 2, 3, 4, 5, 6};
int [] destination = new int[6];
Output:
// iterate and copy elements from source to destination
for (int i = 0; i < source.length; ++i) 1,2,3,4,5,6
{
destination[i] = source[i];
}
source[0]=-1; // modifying element of source array
for (int i = 0; i < destination.length; ++i)
{
System.out.println(destination[i]);
}
}
}
Single Dimensional Array in Java
3. Copying Arrays Using arraycopy() method
• In Java, the System class contains a method named arraycopy() to copy arrays.
This method is a better approach to copy arrays than the above two.
• The arraycopy() method allows you to copy a specified portion of the source array
to the destination array.
• Syntax: arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
Here,
• src - source array you want to copy
• srcPos - starting position (index) in the source array
• dest - destination array where elements will be copied from the source
• destPos - starting position (index) in the destination array
• length - number of elements to copy
Single Dimensional Array in Java
import java.util.Arrays; Arrays.toString() is used to
convert an array of any type to
class arrcopy4 a human-readable string
{ representation.
public static void main(String[] args)
{ The method returns a string
int[] n1 = {2, 3, 12, 4, 12, -2}; representation of the
// Creating n2 array of having length of n1 array elements in the array,
enclosed in square brackets
int[] n2 = new int[n1.length];
([]), with each element
// Creating n3 array of having length 5 separated by commas and
int[] n3 = new int[5]; spaces.
1. Write a program to read 5 numbers into an array and arrange them in ascending
order.
2. Write a program to read 6 numbers into an array and print the duplicate
numbers.
3. Write a program to find Second Largest Number in an Array.
4. Write a program to read n numbers into an array and find the sum of all the
elements.
5. Write a program to copy one array into another array.
MULTI DIMENSIONAL ARRAY
Multidimensional Array in Java
Multi-dimensional Array ( 2 – D Array) :
• Here, data is stored in a row and column-based index (also known as matrix
form)..