University of Hargeisa: Course: Computer Programming I (Java)
University of Hargeisa: Course: Computer Programming I (Java)
Lecture Seven
Presented by:
Sahardiid Ahmed Ali
Chapter 5
Arrays
Chapter 5 Slide 2
Learning Objectives
At the end of the lesson student should be able to:-
Chapter 5 Slide 3
Java Array
Chapter 5 Slide 4
Java Arrays
Chapter 5
Slide 5
Advantages
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.
Chapter 5 Slide 6
Declaring Array Variables
To declare Arrays in Java ,we use the fallowing ways:-
Chapter 5
Slide 7
Declaring Array Variables
To use an array in a program, you must declare a variable to
reference the array, and you must specify the type of array the
variable can reference.
There are two ways:-
int [] x;
String [] names;
or
int x [];
String names [];
Chapter 5
Slide 8
How to Declare Array
Syntax:
Chapter 5
Slide 9
Initializing Arrays
Example:
double[] reading = {3.3, 15.8, 9.7};
double[] reading = new double[3];
reading[0] = 3.3;
reading[1] = 15.8;
reading[2] = 9.7;
Chapter 5
Slide 10
We can use an array literal - place the values in a comma-
separated list, inside curly braces.
Example:
String[] cars = {"Zuki Zuki", "Vitiz", "One10", "Veroza"};
Chapter 5
Slide 11
Access the Elements of an Array
You access an array element by referring to the index
number.
This statement accesses the value of the first element in cars:
Example:-
String[] cars = {"Zuki Zuki", "Vitiz", "One10", "Veroza"};
System.out.println(cars[0]);
// Zuki Zuki
Chapter 5
Slide 12
Creating and Accessing Arrays
follows:
double[] temperature = new double[7];
Chapter 5
Slide 13
Chapter 5 Slide 14
Types of Array in Java
Multidimensional Array
Chapter 5 Slide 15
Multidimensional Array
Chapter 5 Slide 16
Declaring of the 2-D array in Java:
Chapter 5 Slide 17
Single Dimensional Array in Java
dataType arr[];
Chapter 5 Slide 18
package javaapplication26;
a[0]=10;//initialization
Example
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
System.out.println(a[i]);
}
Chapter 5 } Slide 19
Instantiation of an Array in Java
arrayRefVar=new datatype[size];
Example:-
Chapter 5 Slide 20
package javaapplication26;
public class JavaApplication26 {
public static void main(String[] args) {
Example:-
o dataType[][] arrayRefVar;
o dataType arrayRefVar[][];
Chapter 5 Slide 22
Multidimensional Array in Java
dataType []arrayRefVar[];
Chapter 5 Slide 23
Example to instantiate Multidimensional Array in Java
Chapter 5 Slide 24
package javaapplication30;
System.out.print(a[0][0]);
System.out.println(a[0][1]);
System.out.print(a[1][0]);
System.out.println(a[1][1]);
Chapter 5 Slide 25
} } }
package javaapplication26;
public class JavaApplication26 {
public static void main(String[] args) {
//declaring and initializing 2D array
Example int arr[][]={{1,2,3},{4,5,6}};
//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(); }
Chapter 5 }} Slide 26
Copying a Java Array
Chapter 5 Slide 27
package javaapplication26;
public class JavaApplication26 {
Chapter 5
Thanks