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

PROG6112 Chapter 9 - Advanced Array Concepts

Chapter 9 covers advanced array concepts, focusing on sorting algorithms such as Bubble Sort and Insertion Sort, as well as the use of multi-dimensional and ragged arrays. It also introduces the Arrays class for array manipulation and the ArrayList class for dynamic array management. Additionally, the chapter discusses creating enumerations in Java for fixed sets of values.

Uploaded by

Khumo Ramerafe
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

PROG6112 Chapter 9 - Advanced Array Concepts

Chapter 9 covers advanced array concepts, focusing on sorting algorithms such as Bubble Sort and Insertion Sort, as well as the use of multi-dimensional and ragged arrays. It also introduces the Arrays class for array manipulation and the ArrayList class for dynamic array management. Additionally, the chapter discusses creating enumerations in Java for fixed sets of values.

Uploaded by

Khumo Ramerafe
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/ 49

PROG6112

Chapter 9 :

Advanced Array Concepts


Sorting in Arrays
• Sorting
– process of arranging a series of objects in some logical
order.
– Ascending order – Lowest value to highest value
– Descending order – Highest value to lowest value
– Involves comparing two values and swapping them
depending on the out come of the comparison

Programming 1B 2
Programming 1B 3
Sorting in Arrays cont.
• To sort any two values, valA and valB, in ascending
order so that valA is always the lower value:

if(valA > valB){


temp = valA;
valA = valB;
valB = temp;
}

Programming 1B 4
Bubble Sort Algorithm
• Place the original, unsorted values in an array, such
as the following:
int[] someNums = {88, 33, 99, 22, 54};
– Compare 88 and 33. They are out of order. Swap them. The
list becomes 33, 88, 99, 22, 54.
– Compare the second and third numbers in the list—88 and
99. They are in order. Do nothing.
– Compare the third and fourth numbers in the list—99 and
22. They are out of order. Swap them. The list becomes 33,
88, 22, 99, 54.
– Compare the fourth and fifth numbers—99 and 54. They
are out of order. Swap them. The list becomes 33, 88, 22,
54, 99.
Programming 1B 5
Bubble Sort Algorithm cont.
• Code used this far is as follows:

Programming 1B 6
Bubble Sort Algorithm cont.

Ascending Bubble Sort Algorithm

Programming 1B 7
Bubble Sort Algorithm cont.

Running Bubble Sort Algorithm


Programming 1B 8
Bubble Sort Algorithm cont.

Efficient Ascending Bubble Sort Algorithm


Programming 1B 9
Bubble Sort Algorithm cont.

Alternative Bubble Sort Algorithm

Programming 1B 10
Sorting Arrays of Objects

Programming 1B 11
Sorting Arrays of Objects cont.

Programming 1B 12
Insertion Sort Algorithm
Sorting {5, 2, 4, 6, 1 ,3} in ascending order

Programming 1B 13
Insertion Sort Algorithm cont.

Programming 1B 14
Insertion Sort Algorithm cont.

Programming 1B 15
Insertion Sort Algorithm cont.

Programming 1B 16
EXERCISE 1
• The mean of a list of numbers is its arithmetic
average. The median of a list is its middle value when
the values are placed in order. For example, if a list
contains {1, 4, 7, 8,10} , then the mean is 6 and the
median is 7.
– Write an application called MeanMedian that allows you
to enter five integers and displays the values, their mean,
and their median.

Programming 1B 17
EXERCISE 2
A. Radio station JAVA wants a class to keep track of
recordings it plays. Create a class named Recording
that contains fields to hold methods for setting and
getting a Recording’s title, artist, and playing time in
seconds. Save the file as Recording.java.
B. Write an application that instantiates five Recording
objects and prompts the user for values for the data
fields. Then prompt the user to enter which field
the Recordings should be sorted by—song title,
artist, or playing time. Perform the requested sort
procedure, and display the Recording objects. Save
the file as RecordingSort.java.
Programming 1B 18
More on arrays

Programming 1B 19
Multi-dimentional Arrays
• Two-dimensional arrays have two or more columns
of values, as shown below.

• The two dimensions represent the height and width


of the array. Another way to picture a two-
dimensional array is as an array of arrays.

Programming 1B 20
Multi-dimentional Arrays cont.
• Creating an array named someNumbers that holds
three rows and four columns:
int[][] someNumbers = new int[3][4];

• The array someNumbers consists of three rows which


are themselves viewed as one-directional arrays :
• Row 1: someNumber[0]
• Row 2: someNumber[1]
• Row 3: someNumber[2]

Programming 1B 21
Multi-dimentional Arrays cont.

Programming 1B 22
Multi-dimentional Arrays cont.
Declaring a 2-dimensional array

Programming 1B 23
Multi-dimensional Arrays cont.
Declaring and printing out a 2-dimensional array

Programming 1B 24
Multi-dimentional Arrays cont.

Programming 1B 25
Multi-dimentional Arrays cont.
• Looping through a 2D Array

Programming 1B 26
Multi-dimentional Arrays cont.
• Populating a 2D Array with values from keyboard

Programming 1B 27
Multi-dimentional Arrays cont.
• Calculating a total of values in a 2D array:

Programming 1B 28
Exercise
• Write an application that stores at least four
different course names and meeting days and times
in a two-dimensional array. Allow the user to enter a
course name (such as “CIS 110”), and display the day
of the week and time that the course is held (such as
“Th 3:30”). If the course does not exist, display an
error message. Save the file as Schedule.java.

Programming 1B 29
Passing a Two-Dimensional Array
to a Method
• There is no difference between passing a single or
two-dimensional array as an argument to a method.
• The method must accept a two-dimensional array as
a parameter.

Programming 1B 30
Programming 1B 31
Understanding Ragged Arrays
• When the rows of a two-dimensional array are of
different lengths, the array is known as a ragged array.
• You can create a ragged array by creating a two-
dimensional array with a specific number of rows, but no
columns.
int [][] ragged = new int [4][];
• Then create the individual rows.
ragged[0] = new int [3];
ragged[1] = new int [4];
ragged[2] = new int [5];
ragged[3] = new int [6];

Programming 1B 32
Understanding Ragged Arrays cont.

Programming 1B 33
Using the Arrays Class
• Java provides an Arrays class, which contains many
useful methods for manipulating arrays.
• Below are some of the useful methods of the Arrays
class.

Programming 1B 34
Using the Arrays Class cont.
• Methods in the Arrays class are static methods,
which means you use them with the class name
without instantiating an Arrays object

Programming 1B 35
Using the Arrays Class cont.

Programming 1B 36
Using the Arrays Class cont.

Programming 1B 37
Using the ArrayList class
• Used to create containers that store lists of objects
• It is dynamically resizable:
– Its size can change during program execution
– You can add an item at any point in an ArrayList container,
and the array size expands automatically to accommodate
the new item.
– You can remove an item at any point in an ArrayList
container, and the array size contracts automatically.
• Found in the java.util package:
– import java.util.ArrayList;
– import java.util.*;
Programming 1B 38
Using the ArrayList class cont.
• Declare an ArrayList:
ArrayList names = new ArrayList();
• Default size or capacity of ArrayList is 10
• You can declare and specify capacity:
ArrayList names = new ArrayList(20);

Programming 1B 39
Using the ArrayList class cont.
• Useful ArrayList methods include the following:

Programming 1B 40
Using the ArrayList class cont.

Programming 1B 41
Using the ArrayList class cont.
• Creating an ArrayList to store multiple arrays:

Programming 1B 42
Using the ArrayList class cont.

Programming 1B 43
Using the ArrayList class cont.
• You can sort an ArrayList using the Collections.sort()
method and providing the ArrayList as the argument
example: Collections.sort(names);

Programming 1B 44
Creating Enumerations
• A programmer-created data type with a fixed set of
values is an enumerated data type.
• An enum type is a special data type that enables for a
variable to be a set of predefined constants
• Because they are constants, the names of an enum
type's fields are in uppercase letters.
• We define an enum type by using the enum keyword.
For example, you would specify a days-of-the-week
enum type as:
public enum Day {
SUN, MON, TUES, WED, THUR, FRI, SAT
}
Programming 1B 45
Creating Enumerations cont.

enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG,
SEP, OCT, NOV, DEC};

• Declare variables of type Month:


Month birthMonth;
• Assign any of the enum constants to the variable.
birthMonth = Month.MAY;
• An enumeration type like Month is a class, and its
enum constants act like objects instantiated from the
class
Programming 1B 46
Creating Enumerations cont.

Programming 1B 47
Creating Enumerations cont.

Programming 1B 48
Programming 1B 49

You might also like