ITC 1233 - OBJECT ORIENTED
PROGRAMMING
ARRAYS IN JAVA
Ms.Nirasha Kulasooriya,
Lecturer, Dept. of ICT, FOT,USJ.
DO YOU KNOW WHAT IS AN ARRAY?
CAN YOU RECALL YOUR MEMORY IN DATA
TYPES?
RECALLING…
PRIMITIVE DATA TYPES
• The basic types of data.
Ex: int, float, double, boolean, char and also byte, long,
short.
• Mainly used to store values of the variable’s data type.
NON PRIMITIVE DATA TYPE
• Any instantiable class
Ex: strings, arrays, objects
• Used to store the addresses of an object.
ARRAY
• Provide a way to store and organize multiple values of the
same type. (Same data type)
• Immutable data type.
• Making it easier to work with large sets of data.
• The elements of an array can be either primitive types or
reference types.
• The position number of the element can be called as index
or subscript.
ARRAY
• Arrays are objects, so they’re considered non-primitive or
reference types.
• There are 2 types of arrays:
• One-dimensional arrays
• Multi-dimensional arrays
SO, WHAT WOULD BE THE MAIN
IMPORTANCE OF USING ARRAYS IN
PROGRAMMING?
EXAMPLE: AN ARRAY
DECLARING/ CREATING AN ARRAY
● Using primitive data types
● Using ‘new’ keyword
Syntax: dataType[] arrayName;
int[] numbers; // Declare an array of integers
DECLARING/ CREATING AN ARRAY
● Using reference data types.
● Using ‘’new’’ keyword.
Syntax: dataType[] arrayName;
String[] names; // Declare an array of strings
String[] names = {"Alice", "Bob", "Charlie"};
// Declare and initialize an array of strings
DECLARING AND INITIALIZING AN ARRAY
Syntax: arrayName = new dataType[length];
● int[] numbers = new int[5]; // Declare and initialize an array of integers
● int[] numbers = {1, 2, 3, 4, 5}; // Declare and initialize an array
of integers in one line
DECLARING AND INITIALIZING AN ARRAY
Syntax: arrayName = new dataType[length];
Ex:
String[] strArray = new String [5];
Student[] stdArray = new Student [20];
DECLARING AND INITIALIZING AN ARRAY
INITIALIZING AN ARRAY
If an array is created with the new keyword, all the elements of array will be
set to the default values of the data type of the array.
Mainly there are two ways of initializing an array with values.
1. Initializer list
Declaration and initialization in the same statement.
2. Array-access expression
Array-access expression can be used to initialize, read and also
change the values of an array using the index.
INITIALIZING AN ARRAY USING AN INITIALIZER LIST
SUBSCRIPT/ INDEX
• Index is the positional number of elements in an array.
• An array’s first element’s index is 0.
• The first element in every array is the zeroth element.
ACCESSING ARRAY VALUES
• To initialize, read or later change a value of an array, the
array-access expression can be used.
Syntax:
Ex:
EXAMPLE: ACCESSING ARRAY VALUES
ACTIVITY 01
• Declare an integer array named ages.
• Initialize the ages array with a size of 4 using a proper repetition
statement.
• Declare and initialize a string array named fruits with the values
"Apple," "Orange," "Banana," and "Grapes."
• Access and print the first element of the ages array.
• Access and print the third element of the fruits array.
• Write a loop to traverse the ages array and print each element.
LENGTH OF AN ARRAY
length – is an instance variable in an array object which returns
the current array’s length.
ToString()
toString – is a class method in the Arrays class which returns a
string representation of an array. To use the Arrays class,
java.util.Arrays should be imported.
FOR LOOP AND ARRAYS
ENHANCED FOR LOOP (FOREACH LOOP)
Syntax:
for (type variable : array_name) {
...
}
TRY TO DETECT THE DIFFERENCE
FOR LOOP VS ENHANCED FOR LOOP
MULTI-DIMENSIONAL ARRAYS
• A multidimensional array is an array containing one or more
arrays. A multi-dimensional (2D) array can be declared,
created and used as follows.
MULTI-DIMENSIONAL ARRAY
• To declare a multi-dimensional (2D)array and initialize it using
an initializer list, add each array within its own set of curly
braces.
MULTI-DIMENSIONAL ARRAY
• Declaration
dataType[][] arrayName;
• Initialization
arrayName = new dataType[rowLength][colLength];
• Accessing the elements
arrayName[rowIndex][colIndex];
COMMON OPERATIONS IN AN ARRAY
• Getting first and last elements of an array
int firstItem = array[0];
int lastItem = array[array.length - 1];
• Get random value from an array
int anyValue = array[new Random().nextInt(array.length)];
COMMON OPERATIONS IN AN ARRAY
• Append a new item into an array
int[] newArray = Arrays.copyOf(array, array.length + 1);
newArray [newArray.length - 1] = newItem;
• Comparing two arrays
boolean areEqual = Arrays.equals(array1, array2);
ACTIVITY 02
• Declare a 2D array named matrix with 3 rows and 3 columns.
• Initialize the matrix array with values of your choice.
• Access and print the element at the second row and third
column of the matrix array.
ANY QUESTIONS?