0% found this document useful (0 votes)
5 views

Slaid FP301 OOP Topic 2.3 Array UPLOAD

Uploaded by

Azrol Hisham
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)
5 views

Slaid FP301 OOP Topic 2.3 Array UPLOAD

Uploaded by

Azrol Hisham
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

2.

0 FUNDAMENTALS OF THE JAVA


PROGRAMMING LANGUAGE
2.3 Write program using branching statements

©AZROL HISHAM JTMK PSIS


2.3.4 Define an array.
2.3.5 Create reference arrays of objects in Java program
2.3.6 Initialize elements of arrays.
2.3.7 Pass and array to methods.
2.6.8 Write program using single and multidimensional array

©AZROL HISHAM JTMK PSIS 2


2.6.1 Define an array
Definition: An array is a collection of elements of the same data type

@
An array is a container object that holds a fixed number of
values of a single type

©AZROL HISHAM JTMK PSIS 3


An array of ten elements

Each item in an array is called an element, and each element is


accessed by its numerical index.

As shown in the above illustration, numbering begins with 0.

The 9th element, for example, would therefore be accessed at index 8.

©AZROL HISHAM JTMK PSIS 4


Two Dimensional Array

©AZROL HISHAM JTMK PSIS 5


A typical integer array is shown in figure below:-

Numbers

100
150

49

225

©AZROL HISHAM JTMK PSIS 6


2.6.2 Create reference arrays of objects in Java program
When we declare an array variable, the code creates a variable that
can hold the reference to an array object.

For example, int[] list; creates a variable named list of type int[ ].
This variable is capable of referring to an array of ints, but initially its
value is null

list
It does not create the array object or allocate space for
array elements.
©AZROL HISHAM JTMK PSIS 7
Declaring a One-Dimensional Array
• Syntax: type [] array_identifier;
or
type array_identifier [] ;
• Examples:
char [] status; int [] ages; Shirt [] shirts; String [] names;

The square brackets may appear as part of the type at the beginning
of the declaration or as part of the array identifier (after identifier)
It is illegal to specify the size of an array during declaration.

©AZROL HISHAM JTMK PSIS 8


After declaring, we must create the array (Instantiation) and specify its
length with a constructor statement

A constructor is a method that is called to create a certain object.

To instantiate (or create) an array, write the new keyword, followed by


the square brackets containing the number of elements you want the
array to have.

©AZROL HISHAM JTMK PSIS 9


Instantiating a One-Dimensional Array
• Syntax: array_identifier = new type [length];
• The statement will allocate space for the array element

(5) list.length
0 list(0) The array objects contains
The statement list=new int[5];
create an array that can hold five five integers which are refer
0 list(1) to as list[0],list[1] and so on.
ints and set the list to refer to its.
0 list(2) Its also contain list.length
which give the number of
0 list(3) item in the array. list.length
can’t be change
0 list(4)

©AZROL HISHAM JTMK PSIS 10


•Examples allocate memory for array:
status = new char [20];
ages = new int [5];

•To create an array involve two step, first declare array variable, second
step allocate a memory for array.
Syntax to declare array:
type [] array_identifier; OR type array_identifier [] ;
Syntax to allocate memory for array
array_identifier = new type [length];

©AZROL HISHAM JTMK PSIS 11


This two step can be simplified as follows:
Datatype [] array_identifier = new Datatype [length];

Example:
char [] status = new char [20];
int[] ages = new int [5];
float[] floatNum = new float[20];

©AZROL HISHAM JTMK PSIS 12


Declaring a Two-Dimensional Array
• Syntax: type [][] array_identifier;
• Example: int [][] yearlySales;

Instantiating a Two-Dimensional Array


• Syntax: array_identifier = new type [number_of_arrays] [length];
• Example: yearlySales = new int[5][4];
// Instantiates a two-dimensional array: 5 arrays of 4 elements each

©AZROL HISHAM JTMK PSIS 13


Syntax combination of declare and instantiate
Datatype array_identifier[][] = new Datatype[Row][Column];

Example
int marks_table[][] = new int[5][3];
float hutangKau [][] = new float[10][5];
double belanjaRumah [][] = new double[5][4];

©AZROL HISHAM JTMK PSIS 14


2.6.3 Initialize elements of arrays

Initializing a One-Dimensional Array


• Syntax: array_identifier[index] = value;
• Examples:
ages[0] = 19;
ages[1] = 42;
ages[2] = 92;
ages[3] = 33;
ages[4] = 46;
shirts[0] = new Shirt();
shirts[1] = new Shirt(‘G’);
shirts[2] = new Shirt(‘G’, 1000);
©AZROL HISHAM JTMK PSIS 15
Declaring, Instantiating, and Initializing One-Dimensional Arrays
• Syntax:
type [] array_identifier = {comma-separated list of values or
expressions};
• Examples:
int [] ages = {19, 42, 92, 33, 46};
Shirt [] shirts = {new Shirt(), new Shirt(‘G’), new Shirt(‘g’,1000)};
• Error:
int [] ages;
ages = {19, 42, 92, 33, 46};

©AZROL HISHAM JTMK PSIS 16


Using for loop to key in and initializing an
array

for(int i=0;i<5;i++)
{
System.out.print("Enter the marks: ");
str = stdin.readLine();
marks[i] = Integer.parseInt(str);
}

©AZROL HISHAM JTMK PSIS 17


Using for loop to access the array values

for (int i=0;i<5;i++)


{
System.out.println("Marks:"+marks[i]);
}

©AZROL HISHAM JTMK PSIS 18


Initializing a Two-Dimensional Array
Example:
yearlySales[0][0] = 1000;
yearlySales[0][1] = 1500;
yearlySales[0][2] = 1800;
yearlySales[1][0] = 1000;
yearlySales[2][0] = 1400;
yearlySales[3][3] = 2000;

©AZROL HISHAM JTMK PSIS 19


• You can use the row and column array index to
access the array elements.
• For example, if you need to print the value stored in
first row and second column of the array marks_table,
(i.e. chemistry mark of student 2) the code will be :
Example
System.out.println(marks_table[1][2]);

©AZROL HISHAM JTMK PSIS 20


A nested for loop can be used to enter data in a 2D array
For example,

for (int x=0;x<2;x++){


for(int y=0;y<3;y++){
m = stdin.readLine();
marks_table[x][y] =
Integer.parseInt(m);
}
}

©AZROL HISHAM JTMK PSIS 21


Example to read data from an array

for(int x=0;x<2;x++){
for(int y=0;y<3;y++){
System.out.println("Marks:” +
marks[x][y]);
}
}

©AZROL HISHAM JTMK PSIS 22


2.6.4 Pass array to methods.
• When discussing arguments/parameters and methods,
we talked about passing-by-value.
• Copies of argument values are sent to the method,
where the copy is manipulated and in certain cases,
one value may be returned.
• While the copied values may change in the method,
the original values in main did not change (unless
purposely reassigned after the method).

©AZROL HISHAM JTMK PSIS 23


• Arrays are passed-by-reference.
• Passing-by-reference means that when an array is passed
as an argument, its memory address location is actually
passed, referred to as its "reference".

• In this way, the contents of an array CAN be changed


inside of a method, since we are dealing directly with the
actual array and not with a copy of the array.

Syntax to pass an Array: methodName(arrayName);

©AZROL HISHAM JTMK PSIS 24


Syntax to set array as parameter in method:
public methodName(dataType[] arrayName){
//body of method;
}

Syntax to pass an Array: methodName(arrayName);

Syntax to returning an array:


public arrayType methodName(){

return arrayName;
}
©AZROL HISHAM JTMK PSIS 25
Example:
int [ ] num = {1, 2, 3};
testingArray(num); //Method call
System.out.println("num[0] = " + num[0] + "\n num[1] = " +
num[1] + "\n num[2] =" + num[2]);
...

//Method for testing


public static void testingArray(int[ ] value){
value[0] = 4;
value[1] = 5;
value[2] = 6;
} ©AZROL HISHAM JTMK PSIS 26
Output:
num[0] = 4
num[1] = 5
num[2] = 6
(The values in the array have been changed.
Notice that nothing was "returned".)

©AZROL HISHAM JTMK PSIS 27


2.6.5 Return array to methods
A method may also return an array. For example, the
method shown below returns an array that is the reversal of
another array:

public static int[] reverse(int[] list) {


int[] result = new int[list.length];

for (int i = 0; i = result.length - 1; i < list.length; i++, j--) {


result[j] = list[i];
}
return result;
} ©AZROL HISHAM JTMK PSIS 28
2.6.6 Write program using single & multidimensional
array.
import java.io.*;
public class Array {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
int int2DArray[][]=new int[2][3];
for(int i=0;i<5;i++){ //Accepting the number
System.out.print("Enter an integer: ");
str = br.readLine();
marks[i] = Integer.parseInt(str);
}
for(int i=0;i<5;i++){//Displaying the array
System.out.println("Integer "+(i+1)+" :"+marks[i]);
}
}
}
©AZROL HISHAM JTMK PSIS 29
import java.io.*;
public class TwoDimension {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
int int2DArray[][]=new int[2][3];
for(int x=0;x<2;x++){ //Accepting the number
for(int y=0;y<3;y++){
System.out.print("Enter an integer: ");
str = br.readLine();
int2DArray[x][y] = Integer.parseInt(str);
}
}
for(int x=0;x<2;x++){
for(int y=0;y<3;y++){
System.out.println("Integer indeks: [ "+x+"][ "+y+"] :"+int2DArray[x][y]);
}
}
}
} ©AZROL HISHAM JTMK PSIS 30

You might also like