0% found this document useful (0 votes)
65 views30 pages

University of Hargeisa: Course: Computer Programming I (Java)

This document provides a summary of Chapter 5 on Arrays from a Computer Programming I lecture at the University of Hargeisa. The chapter covers defining and comparing the advantages and disadvantages of arrays in Java, declaring and initializing different types of arrays, accessing array elements, and copying arrays. Examples are provided for declaring single and multi-dimensional arrays, initializing arrays, accessing elements, and copying an array using the arraycopy method. The chapter aims to teach students how to work with arrays in Java programming.

Uploaded by

Abdirahman Shire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views30 pages

University of Hargeisa: Course: Computer Programming I (Java)

This document provides a summary of Chapter 5 on Arrays from a Computer Programming I lecture at the University of Hargeisa. The chapter covers defining and comparing the advantages and disadvantages of arrays in Java, declaring and initializing different types of arrays, accessing array elements, and copying arrays. Examples are provided for declaring single and multi-dimensional arrays, initializing arrays, accessing elements, and copying an array using the arraycopy method. The chapter aims to teach students how to work with arrays in Java programming.

Uploaded by

Abdirahman Shire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

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:-

 Define Java Array

 Compare Advantage and Disadvantage of an Array in Java

 How to Declare Array in Java

 How to Access Elements of an Array in Java

 Explain Types of Array in Java

 Define Copying a Java Array in Java

 Examples for each Point in Java

Chapter 5 Slide 3
Java Array

Array is a collection of similar type of elements which has


contiguous memory location.

Java array is an object which contains elements of a similar data


type.

Chapter 5 Slide 4
Java Arrays

Arrays are used to store multiple values in a single variable, instead


of declaring separate variables for each value.

An array is a special kind of object used to store a collection of data.


An array differs from the other objects you have seen in two ways:
1. The array, which stores a fixed-size
2. Items in an array have the same data type

Chapter 5
Slide 5
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.

Chapter 5 Slide 6
Declaring Array Variables
To declare Arrays in Java ,we use the fallowing ways:-

There are two ways:-

1. Data Type Array Size Array Name Semi-colon


2. Data Type Array Name Array Size Semi-colon

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:

dataType[] arrayRefVar; // preferred way.


To declare an array, define the variable type with square
brackets:

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"};

To create an array of integers, you could write:


int[] myNum = {10, 20, 30, 40};

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

For example, an array consisting of a collection of seven


variables of type double can be created as

follows:
double[] temperature = new double[7];

Chapter 5
Slide 13
Chapter 5 Slide 14
Types of Array in Java

There are two types of array:-


Single Dimensional Array

Multidimensional Array

Chapter 5 Slide 15
Multidimensional Array

An array with more than one dimension is known as a


multi-dimensional array.
An array with more than one dimension is known as a
multi-dimensional array.

Chapter 5 Slide 16
Declaring of the 2-D array in Java:

Any 2-dimensional array can be declared as follows:


Syntax:
Data_type array_name[][]; (OR
Data_type[][] array_name;

Initialization of 2-D array in Java:


Data_type[][] array_Name = new data_type[no_of_rows][no_of_columns];

Chapter 5 Slide 17
Single Dimensional Array in Java

Syntax to Declare an Array in Java


1. Data Type Array Size Array Name Semi-colon
2. Data Type Array Name Array Size Semi-colon

 dataType[] arr; (or)

 dataType []arr; (or)

 dataType arr[];

Chapter 5 Slide 18
package javaapplication26;

public class JavaApplication26 {

public static void main(String[] args) {


int a[]=new int[5];//declaration and instantiation

a[0]=10;//initialization
Example
a[1]=20;

a[2]=70;
a[3]=40;

a[4]=50;

for(int i=0;i<a.length;i++)//length is the property of array

System.out.println(a[i]);

}
Chapter 5 } Slide 19
Instantiation of an Array in Java

arrayRefVar=new datatype[size];

Example:-

int a[]={33,3,4,5};//declaration, instantiation and


initialization

Chapter 5 Slide 20
package javaapplication26;
public class JavaApplication26 {
public static void main(String[] args) {

Example int a[]={1,2,3,4};//declaration, instantiation


and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the
property of array
System.out.println(a[i]);
Chapter 5 Slide 21
}}
Syntax of Multidimensional Array in Java

Data Type Mult Array Size Array Name Semi-colon

Data Type Array Name Mult Array Size Semi-colon

Example:-
o dataType[][] arrayRefVar;

o dataType arrayRefVar[][];

Chapter 5 Slide 22
Multidimensional Array in Java

Syntax to Declare Multidimensional Array in Java


dataType[][] arrayRefVar; (or)

dataType arrayRefVar[][]; (or)

dataType []arrayRefVar[];

Chapter 5 Slide 23
Example to instantiate Multidimensional Array in Java

int[][] arr=new int[3][3]; //3 row and 3 column

Chapter 5 Slide 24
package javaapplication30;

public class JavaApplication30 {

public static void main(String[] args) {

int[][] a={{10,20},{30,40}};//declaration and


Example
initialization

System.out.println("Two dimensional array elements


are");

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

We can copy an array to another by the arraycopy()


method of System class.
Syntax of arraycopy method
System.arraycopy(Array Name, Intial Position, copyTo,
Intial value, Last value);
System.out.println(String.valueOf(copyTo)); }

Chapter 5 Slide 27
package javaapplication26;
public class JavaApplication26 {

public static void main(String[] args) {


//declaring a source array
char[] copyFrom = { 'a', 'b', 'S', 'o', 'm', 'a', 'l',
Example
'i', 'l', 'a', 'n', 'd' };
//declaring a destination array

char[] copyTo = new char[10];


//copying array using System.arraycopy() method
System.arraycopy(copyFrom, 2, copyTo, 0, 10);
//printing the destination array
System.out.println(String.valueOf(copyTo)); }
Chapter 5 } Slide 28
Any Question??

Chapter 5
Thanks

You might also like