Array and Enum
Array and Enum
• Introduction
• Array Creation
• Array Initialization
• Enumerations
Array
• Definition:
An array is a finite collection of variables of the same type that
are referred to by a common name.
• Arrays of any type can be created and may have one or more
dimensions.
• A specific element in an array is accessed by its index
(subscript).
• Array elements are stored in contiguous memory locations.
• Examples:
• Collection of numbers
• Collection of names
More points
• In Java all arrays are dynamically allocated.
• Since arrays are objects in Java, we can find their length using
the object property length.
• The direct superclass of an array type is Object.
• If we try to access array outside of its index then
ArrayIndexOutOfBounds Exception will be raised
One-Dimensional Arrays
• A one-dimensional array is a list of variables of same
type.
• The general form of a one-dimensional array
declaration is:
type [] arr_ref_var; OR
type [] arr_ref_var= new type[size];
Example:
int [] num = new int [10];
It will create an array of 10 integers.
Syntax and Example
This will declare an array named ‘marks’ of type ‘int’. But no memory is
allocated to the array.
Allocation of memory:
variable-name = new data-type[size];
eg. marks = new int[5];
This will allocate memory of 5 integers to the array ‘marks’ and it can store
upto 5 integers in it. ‘new’ is a special operator that allocates memory.
Another Example of array
Memory allocation
Accessing elements in the array:
• Specific element in the array is accessed by specifying
name of the array followed the index of the element.
• All array indexes in Java start at zero.
variable-name[index] = value;
Example:
marks[0] = 10;
This will assign the value 10 to the 1st element in the array.
marks[2] = 863;
This will assign the value 863 to the 3rd element in the array.
Example
STEP 1 : (Declaration)
int marks[];
marks null
STEP 2: (Memory Allocation)
marks = new int[5];
marks 0 0 0 0 0
marks[0] marks[1] marks[2] marks[3] marks[4]
• Default values:
– zero (0) for numeric data types,
– \u0000 for chars and
– false for Boolean types
}
}
Printing array elements using for
each loop
We can also print the Java array using for-each loop. The Java
for-each loop prints the array elements one by one. It holds an
array element in a variable, then executes the body of the loop.
The syntax of the for-each loop is given below:
for(data_type variable:array)
{
//body of the loop
}
Example 1
class Example
{
public static void main(String args[])
{
int arr[]={33,3,4,5};
//printing array using for-each loop
for(int i:arr)
System.out.println(i);
}
}
Example 2
import java.util.Scanner;
class Example
{
public static void main(String args[])
{
int i;
String s[] = new String[5];
Scanner ob = new Scanner(System.in);
System.out.println("Enter the 5 strings");
for(i=0;i<s.length;i++)
s[i]=ob.nextLine();
System.out.println("5 strings are");
for(String x : s)
System.out.println(x);
}
}
Exercise
Write a program which prompts the user to enter the
number of elements.Now read the marks of all the
subjects from the user using Scanner class.Write a
method which calculates the percentage of the user.
Multi-Dimensional Array
Multidimensional arrays are arrays of arrays(2D,3D….)
Two-Dimensional arrays are used to represent a table or a
matrix.
A two-dimensional array is actually an array in which each
element is a one-dimensional array.
Declaration:
elementType[][] arrayRefVar; or elementType arrayRefVar[][];
Example: int[][]a; or int a[][];
Creating 2D array:
elementType[][] arrayRefVar=new elementType[n][m];
Example:
int twoD[][] = new int[4][5];
Conceptual View of 2-Dimensional Array
class TwoDimArr
{
public static void main(String args[]) Output:
{
int twoD[][]= new int[4][5]; 01234
int i, j, k = 0; 56789
10 11 12 13 14
for(i=0; i<4; i++)
15 16 17 18 19
for(j=0; j<5; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++)
{
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
• When we allocate memory for a multidimensional
array, we need to only specify the memory for the
first (leftmost) dimension.
In first case, Only one array is created and two references arr1
and arr2 are pointing to the same array. While in second case
two different arrays are created.
Cloning of 1D Array
// Java program to demonstrate
// cloning of one-dimensional arrays
class Test
Output:
{
false
public static void main(String args[]) 123
{
int intArray[] = {1,2,3};
int cloneArray[] = intArray.clone();
// will print false as deep copy is created
// for one-dimensional array
System.out.println(intArray == cloneArray);
for (int i = 0; i < cloneArray.length; i++) {
System.out.print(cloneArray[i]+" ");
}
}
}
Deep copy is created for ID array
Cloning of 2D Array
A clone of a multi-dimensional array (like Object[][]) is a “shallow copy” however,
which is to say that it creates only a single new array with each element array a
reference to an original element array, but subarrays are shared.
// Java program to demonstrate
// cloning of multi-dimensional arrays
class Test Output:
{ false
true
public static void main(String args[])
true
{
int intArray[][] = {{1,2,3},{4,5}};
int cloneArray[][] = intArray.clone();
// will print false
System.out.println(intArray == cloneArray);
// will print true as shallow copy is created
// i.e. sub-arrays are shared
System.out.println(intArray[0] == cloneArray[0]);
System.out.println(intArray[1] == cloneArray[1]);
}
}
Shallow copy created for 2D array
Assignment for Practice