PF Lecture 20,21
PF Lecture 20,21
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;
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];
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
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.
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].
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);
Zeeshan Raza
14
[email protected]
Initializing arrays with random values
Zeeshan Raza
15
[email protected]
Printing Arrays
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];
Zeeshan Raza
22
[email protected]
The arraycopy Utility
arraycopy(sourceArray, src_pos, targetArray, tar_pos, length);
Example:
Zeeshan Raza
23
[email protected]
Passing Arrays to Methods
Zeeshan Raza
27
[email protected]