Professional Documents
Culture Documents
A1386673771 - 21789 - 9 - 2019 - 07. Array and Enum
A1386673771 - 21789 - 9 - 2019 - 07. Array and Enum
• Introduction
• Array Creation
• Array Initialization
• Array as an Argument
• Array as a return type
• Enumerations
Array
• Definition:
• An array is a collection of similar type of elements
that have a contiguous memory location.
• Java array is an object which contains elements of a
similar data type. 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 0 index.
• Advantages
• Code Optimization: It makes the code optimized, we
can retrieve or sort the data efficiently.
• Random access: We can get any data located at an
index position.
• Disadvantages
• Size Limit: We can store only the fixed size of
elements in the array. It doesn't grow its size at
runtime. To solve this problem, collection framework
is used in Java which grows automatically.
Examples
Array of numbers:
10 23 863 8 229
Array of names:
Array of suffixes:
ment tion ness ves
• Types of Array in java:
Example:
int [] num = new int [10];
Syntax
Declaration of array variable:
data-type variable-name[];
eg. int marks[];
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.
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
int a[]={33,3,4,5};
//declaration, instantiation and initialization
Array Initialization
1. data Type [] array_ref_var = {value0, value1, …, value n};
Now read the marks of all the subjects from the user
using Scanner class.
• Example:
char twod1[][] = new char[3][4];
char[][] twod2 = new char[3][4];
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.
Array as Argument
• Arrays can be passed as an argument to any method.
Example:
Example:
public int [] Result (int roll_No, int marks )
Assignment for Practice
class EnumDemo2
{
public static void main(String[] rk)
{
for (Season s : Season.values())
System.out.println(s);
}
}
Initializing specific values to the
enum constants
• The enum constants have initial value that
starts from 0, 1, 2, 3 and so on.
• But we can initialize the specific value to the
enum constants by defining fields and
constructors.
• As specified earlier, Enum can have fields,
constructors and methods.
Enum with Constructor
class EnumExample4
{
enum Season{
WINTER(5), SPRING(10), SUMMER(15), FALL(20);
private int value;
private Season(int value){
this.value=value;
}
}
public static void main(String args[])
{
for (Season s : Season.values())
System.out.println(s+" "+s.value);
}}
• Constructor of enum type is private.
• You can declare abstract methods within an enum. If you do so, all
the enum fields must implement the abstract methods.