TOPIC 6 Â SINGLE DIMENSIONAL ARRAYS
TOPIC 6 Â SINGLE DIMENSIONAL ARRAYS
TOPIC 6 Â SINGLE DIMENSIONAL ARRAYS
Fundamental of
Programming
TOPIC 6 – SINGLE DIMENSIONAL ARRAYS
mfs
Source: pexels.com
OPENING PROBLEM
Read one hundred numbers, compute their average,
and find out how many numbers are above the
average.
OBJECTIVES 1 of 2
• To describe why arrays are necessary in programming.
• To declare array reference variables and create arrays).
• To obtain array size using arrayRefVar.length and know default values in an
array.
• To access array elements using indexes.
• To declare, create, and initialize an array using an array initializer.
• To program common array operations (displaying arrays, summing all elements,
finding the minimum and maximum elements, random shuffling, and shifting
elements).
• To simplify programming using the foreach loops.
OBJECTIVES 2 of 2
• To apply arrays in application development (AnalyzeNumbers, DeckOfCards).
• To copy contents from one array to another.
• To develop and invoke methods with array arguments and return values.
• To define a method with a variable-length argument list.
• To search elements using the linear or binary search algorithm.
• To sort an array using the selection sort approach.
• To use the methods in the java.util.Arrays class.
• To pass arguments to the main method from the command line.
INTRODUCING ARRAYS
Array is a data structure that represents a collection of the same types of
data.
DECLARING ARRAY VARIABLES
• datatype[] arrayRefVar;
AnalyzeNumbers
PROBLEM: DECK OF CARDS (1 of 4)
The problem is to write a program that picks four cards randomly from a deck
of 52 cards. All the cards can be represented using an array named deck,
filled with initial values 0 to 51, as follows:
DeckOfCards
PROBLEM: DECK OF CARDS (2 of 4)
PROBLEM: DECK OF CARDS (3 of 4)
DeckOfCards
PROBLEM: DECK OF CARDS (4 of 4)
This problem builds a foundation for future more interesting and realistic
applications:
See Exercise 20.15.
https://liveexample.pearsoncmg.com/dsanimation/24Point.html
COPYING ARRAYS (1 of 2)
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;
COPYING ARRAYS (2 of 2)
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];
THE arraycopy UTILITY
arraycopy(sourceArray, src_pos, targetArray, tar_pos, length);
Example:
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
PASSING ARRAYS TO METHODS
ANONYMOUS ARRAY
• The statement
printArray(new int[]{3, 1, 2, 6, 4, 2});
• creates an array using the following syntax:
new dataType[]{literal0, literal1, ..., literalk};
• There is no explicit reference variable for the array. Such array is called
an anonymous array.
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.
SIMPLE EXAMPLE
CALL STACK (1 of 2)
When invoking m(x, y), the values of x and y are passed to number and numbers.
Since y contains the reference value to the array, numbers now contains the
same reference value to the same array.
CALL STACK (2 of 2)
When invoking m(x, y), the values of x and y are passed to number and numbers.
Since y contains the reference value to the array, numbers now contains the
same reference value to the same array.
HEAP
The JVM stores the array in an area of memory, called heap, which is used for
dynamic memory allocation where blocks of memory are allocated and freed in an
arbitrary order.
Heap
TestPassArray
EXAMPLE
Stack Heap Stack
Space required for the
Space required for the swapFirstTwoInArray
swap method method
n2: 2 int[] array reference
n1: 1
CountLettersInArray
VARIABLE-LENGTH ARGUMENTS
You can pass a variable number of arguments of the same type to a method.
VarArgsDemo
SEARCHING ARRAYS
Searching is the process of looking for a specific element in an array; for
example, discovering whether a certain score is included in a list of scores.
Searching is a common task in computer programming. There are many algorithms
and data structures devoted to searching. In this section, two commonly used
approaches are discussed, linear search and binary search.
https://liveexample.pearsoncmg.com/dsanimation/LinearSearcheBook.html
FROM IDEA TO SOLUTION (1 of 6)
key > 50 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
low mid high
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
key < 66 list 59 60 66 69 70 79
[7] [8]
key < 59 list 59 60
low high
return -1 - low;
}
THE Arrays.binarySearch METHOD
Since binary search is frequently used in programming, Java provides several
overloaded binarySearch methods for searching a key in an array of int,
double, char, short, long, and float in the java.util.Arrays class. For
example, the following code searches the keys in an array of numbers and an
array of characters.
list [0] list [1] list [2] list [3] ... list [10]
list [0] list [1] list [2] list [3] ... list [10]
list [0] list [1] list [2] list [3] ... list [10]
list [0] list [1] list [2] list [3] ... list [10]
list [0] list [1] list [2] list [3] ... list [10]
...
list [0] list [1] list [2] list [3] ... list [10]
FROM IDEA TO SOLUTION (4 of 6)
FROM IDEA TO SOLUTION (5 of 6)
FROM IDEA TO SOLUTION (6 of 6)
WRAP IT IN A METHOD
THE Arrays.sort METHOD
Since sorting is frequently used in programming, Java provides several
overloaded sort methods for sorting an array of int, double, char, short,
long, and float in the java.util.Arrays class. For example, the following code
sorts an array of numbers and an array of characters.
double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
java.util.Arrays.sort(numbers);
char[] chars = {'a', 'A', '4', 'F', 'D', 'P'};
java.util.Arrays.sort(chars);
java Calculator 2 + 3
java Calculator 2 - 3
java Calculator 2 / 3
java Calculator 2.3
Calculator
COPYRIGHT