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
zeeshanraza@cuisahiwal.edu.pk
Opening Problem
• Read one hundred numbers, compute their
average, and find out how many numbers are
above the average.
Zeeshan Raza
2
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
Declaring Array Variables
• datatype[] arrayRefVar;
Example:
double[] myList;
• datatype arrayRefVar[]; // This style is allowed, but
not preferred
Example:
double myList[];
Zeeshan Raza
4
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
Creating Arrays
Zeeshan Raza
7
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
Initializing arrays with random values
for (int i = 0; i < myList.length; i++)
{
myList[i] = Math.random() * 100;
}
Zeeshan Raza
15
zeeshanraza@cuisahiwal.edu.pk
Printing Arrays
for (int i = 0; i < myList.length; i++)
{
System.out.print(myList[i] + " ");
}
Zeeshan Raza
16
zeeshanraza@cuisahiwal.edu.pk
Summing All Elements
double total = 0;
for (int i = 0; i < myList.length; i++)
{
total += myList[i];
}
Zeeshan Raza
17
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
Random Shuffling
Zeeshan Raza
19
zeeshanraza@cuisahiwal.edu.pk
Shifting Elements
Zeeshan Raza
20
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
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
zeeshanraza@cuisahiwal.edu.pk
Returning an Array from a Method
When a method returns an array, the reference of the array is returned
Zeeshan Raza
27
zeeshanraza@cuisahiwal.edu.pk