0% found this document useful (0 votes)
6 views27 pages

PF Lecture 20,21

The document provides an overview of arrays in programming, including how to declare, initialize, and access array elements. It covers topics such as array length, processing one-dimensional arrays, and passing arrays to methods. Additionally, it explains the differences between passing primitive types and arrays in Java, as well as methods for copying and returning arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

PF Lecture 20,21

The document provides an overview of arrays in programming, including how to declare, initialize, and access array elements. It covers topics such as array length, processing one-dimensional arrays, and passing arrays to methods. Additionally, it explains the differences between passing primitive types and arrays in Java, as well as methods for copying and returning arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CSC103-Programming Fundamentals

Arrays: Declare and Initialize an Array, Accessing Array Elements,


Specifying Array Size during Program Execution, Array Length,
Processing One-Dimensional Arrays.
Declaring Arrays as Formal Parameters to Methods, Arrays as
Parameters to Methods, Methods Returning Arrays, Variable-
Length Argument Lists, and Command-Line Arguments.
Instructors Lecture-20,21
Mr. Zeeshan Raza
Zeeshan Raza
1
[email protected]
Opening Problem
• Read one hundred numbers, compute their
average, and find out how many numbers are
above the average.

Zeeshan Raza
2
[email protected]
Introducing Arrays
• Array is a data structure that represents a
collection of the same types of data
• Once an array is created, its size is fixed
• An array reference variable is used to
access the elements in an array using an
index

Zeeshan Raza
3
[email protected]
Declaring Array Variables
• datatype[] arrayRefVar;
Example:
double[] myList;

• datatype arrayRefVar[]; // This style is allowed, but


not preferred
Example:
double myList[];

Zeeshan Raza
4
[email protected]
Creating Arrays
Create an array by using the new
operator and assign its reference
to the variable

arrayRefVar = new
new datatype[arraySize];
(1) It creates an array using new elementType[arraySize];
(2) It assigns the reference of the newly created array to the
variable arrayRefVar.

Example:
myList = new double[10];
myList[0] references the first element in the array.
myList[9] references the last element in the array.

Zeeshan Raza
5
[email protected]
Declaring and Creating in One Step
• datatype[] arrayRefVar = new datatype[arraySize];

double[] myList = new double[10];

• datatype arrayRefVar[] = new datatype[arraySize];

double myList[] = new double[10];

• To assign values to the elements:

arrayRefVar[index] = value;

Zeeshan Raza
6
[email protected]
Creating Arrays

Zeeshan Raza
7
[email protected]
The Length of an Array
Once an array is created, its size is fixed. It cannot
be changed. You can find its size using

arrayRefVar.length

For example,

myList.length returns 10

Zeeshan Raza
8
[email protected]
Default Values
When an array is created, its elements are
assigned the default value of

0 for the numeric primitive data types,


'\u0000' for char types, and
false for boolean types.

Zeeshan Raza
9
[email protected]
Indexed Variables
The array elements are accessed through the index.
The array indices are 0-based, i.e., it starts from 0 to
arrayRefVar.length-1. In the example myList holds
ten double values and the indices are from 0 to 9.

Each element in the array is represented using the


following syntax, known as an indexed variable:

arrayRefVar[index];
Zeeshan Raza
10
[email protected]
Using Indexed Variables
After an array is created, an indexed variable
can be used in the same way as a regular
variable. For example, the following code
adds the value in myList[0] and myList[1] to
myList[2].

myList[2] = myList[0] + myList[1];

Zeeshan Raza
11
[email protected]
Declaring, creating, initializing Using
the Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
- This shorthand syntax must be in one statement.
This shorthand notation is equivalent to the following
statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;

Zeeshan Raza
12
[email protected]
Processing Arrays
1. Initializing arrays with input values
2. Initializing arrays with random values
3. Printing arrays
4. Summing all elements
5. Finding the largest element
6. Finding the smallest index of the largest element
7. Random shuffling
8. Shifting elements

Zeeshan Raza
13
[email protected]
Initializing arrays with input values
Scanner input = new Scanner(System.in);

System.out.print("Enter " + myList.length + " values: ");

for (int i = 0; i < myList.length; i++)


{
myList[i] = input.nextDouble();
}

Zeeshan Raza
14
[email protected]
Initializing arrays with random values

for (int i = 0; i < myList.length; i++)


{
myList[i] = Math.random() * 100;
}

Zeeshan Raza
15
[email protected]
Printing Arrays

for (int i = 0; i < myList.length; i++)


{
System.out.print(myList[i] + " ");
}

Zeeshan Raza
16
[email protected]
Summing All Elements

double total = 0;
for (int i = 0; i < myList.length; i++)
{
total += myList[i];
}

Zeeshan Raza
17
[email protected]
Finding The Largest Element
double max = myList[0];
for (int i = 1; i < myList.length; i++)
{
if (myList[i] > max)
{
max = myList[i];
}
}
Zeeshan Raza
18
[email protected]
Random Shuffling

Zeeshan Raza
19
[email protected]
Shifting Elements

Zeeshan Raza
20
[email protected]
Copying Arrays
Often, in a program, you need to duplicate an
array or a part of an array. In such cases you
could attempt to use the assignment statement
(=), as follows:

list2 = list1;

Zeeshan Raza
21
[email protected]
Copying Arrays
To copy the contents of one array into another, you have to copy the array’s
individual elements into the other array

Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];

for (int i = 0; i < sourceArrays.length; i++)


targetArray[i] = sourceArray[i];

Zeeshan Raza
22
[email protected]
The arraycopy Utility
arraycopy(sourceArray, src_pos, targetArray, tar_pos, length);

- srcPos and tarPos indicate the starting positions in sourceArray


and targetArray, respectively
- length number of elements copied from sourceArray to
targetArray

Example:

System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

Zeeshan Raza
23
[email protected]
Passing Arrays to Methods

When passing an array to a method, the reference of the array is


passed to the method.

public static void printArray(int[] array) {


for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
Invoke the method

int[] list = {3, 1, 2, 6, 4, 2};


printArray(list);
printArray(new int[]{3, 1, 2, 6, 4, 2});
Zeeshan Raza
24
[email protected]
Pass By Value
Java uses pass by value to pass arguments to a method.
There are important differences between passing a value
of variables of primitive data types and passing arrays.

• For a parameter of a primitive type value, the actual


value is passed. Changing the value of the local
parameter inside the method does not affect the value of
the variable outside the method.

• For a parameter of an array type, the value of the


parameter contains a reference to an array; this reference
is passed to the method. Any changes to the array that
occur inside the method body will affect the original array
that was passed as the argument.
Zeeshan Raza
25
[email protected]
Simple Example
public class Test {
public static void main(String[] args) {
int x = 1; // x represents an int value
int[] y = new int[10]; // y represents an array of int values

m(x, y); // Invoke m with arguments x and y

System.out.println("x is " + x);


System.out.println("y[0] is " + y[0]);
}

public static void m(int number, int[] numbers) {


number = 1001; // Assign a new value to number
numbers[0] = 5555; // Assign a new value to numbers[0]
}
} Zeeshan Raza
26
[email protected]
Returning an Array from a Method
When a method returns an array, the reference of the array is returned

Zeeshan Raza
27
[email protected]

You might also like