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

OOProg21 - Advanced Array Concepts

This document discusses various array concepts including sorting arrays with bubble and insertion sort algorithms, using two-dimensional and multidimensional arrays, the Arrays and ArrayList classes, and creating enumerations. Sorting involves arranging objects in logical order. Two-dimensional arrays have both rows and columns. The Arrays class contains useful methods for manipulating arrays while the ArrayList class is dynamically resizable. An enumerated data type defines a fixed set of values for a data type.

Uploaded by

Eloisa Villaflor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

OOProg21 - Advanced Array Concepts

This document discusses various array concepts including sorting arrays with bubble and insertion sort algorithms, using two-dimensional and multidimensional arrays, the Arrays and ArrayList classes, and creating enumerations. Sorting involves arranging objects in logical order. Two-dimensional arrays have both rows and columns. The Arrays class contains useful methods for manipulating arrays while the ArrayList class is dynamically resizable. An enumerated data type defines a fixed set of values for a data type.

Uploaded by

Eloisa Villaflor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Advanced Array

Concepts
Objectives
• Sort array elements using the bubble sort algorithm
• Sort array elements using the insertion sort algorithm
• Use two-dimensional and other multidimensional
arrays
• Use the Arrays class
• Use the ArrayList class
• Create enumerations

2
Sorting Array Elements Using the
Bubble Sort Algorithm
• Sorting
– The process of arranging a series of objects in some logical
order
• Ascending order
– Begin with the object that has the lowest value
• Descending order
– Begin with the object that has the largest value

3
Sorting Array Elements Using the
Bubble Sort Algorithm (cont’d.)
• Simplest possible sort
– Involves two values that are out of order
– Swap two values
temp = valA; // 16 goes to temp
valA = valB; // 2 goes to valA
valB = temp; // 16 goes to valB

4
Using the Bubble Sort Algorithm
• Bubble sort
– You continue to compare pairs of items, swapping them if
they are out of order
– The smallest items “bubble” to the top of the list,
eventually creating a sorted list

5
Using the Bubble Sort Algorithm
(cont’d.)

6
Using the Bubble Sort Algorithm
(cont’d.)

7
Sorting Arrays of Objects
• You can sort arrays of objects in much the same way
that you sort arrays of primitive types
– Major difference
• Make the comparison that determines whether to swap two array
elements
• Sort based on a particular object field

8
Sorting Array Elements Using the
Insertion Sort Algorithm
• Insertion sort
– A sorting algorithm that enables you to look at each list
element one at a time
– Move items down if the tested element should be inserted
prior to other elements
• Similar to the technique that sorts a group of objects
manually

9
Sorting Array Elements Using the
Insertion Sort Algorithm (cont’d.)

10
Using Two-Dimensional and Other
Multidimensional Arrays
• One-dimensional or single-dimensional array
– An array that you can picture as a column of values
– Elements are accessed using a single subscript
• Two-dimensional arrays
– Have two or more columns of values
– Have rows and columns
– Use two subscripts
– Are often called a matrix or table
int[][] someNumbers = new int[3][4];

11
Using Two-Dimensional and Other
Multidimensional Arrays (cont’d.)

12
Using Two-Dimensional and Other
Multidimensional Arrays (cont’d.)
int[][] rents = { {400, 450, 510},
{500, 560, 630},
{625, 676, 740},
{1000, 1250, 1600} };

13
Passing a Two-Dimensional Array
to a Method
• Pass the array name just as you do with a one-
dimensional array
public static void
displayScores(int[][]scoresArray)

14
Using the length Field with a
Two-Dimensional Array
• The length field holds the number of rows in the
array
rents.length
• Each row has a length field that holds the number
of columns in the row
rents[1].length

15
Understanding Ragged Arrays
• Ragged array
– A two-dimensional array with rows of different lengths
• To create a ragged array:
– Define the number of rows for a two-dimensional array
– Do not define the number of columns in the rows
– Then declare the individual rows

16
Using Other Multidimensional
Arrays
• Multidimensional arrays
– Arrays with more than one dimension
• Create arrays of any size
– Keep track of the order of variables needed as subscripts
– Do not exhaust your computer’s memory

17
Using the Arrays Class
• Arrays class
– Contains many useful methods for manipulating arrays
– static methods
• Use them with the class name without instantiating an Arrays
object
– binarySearch() method
• A convenient way to search through sorted lists of values of
various data types
• The list must be in order

18
Using the Arrays Class (cont’d.)

19
20
21
Using the ArrayList Class
• The ArrayList class provides some advantages
over the Arrays class
– Dynamically resizable
– Can add an item at any point in an ArrayList container
– Can remove an item at any point in an ArrayList
container
• Capacity
– The number of items an ArrayList can hold without
having to increase its size

22
Using the ArrayList Class
(cont’d.)

23
Using the ArrayList Class
(cont’d.)

24
Creating Enumerations
• Enumerated data type
– A programmer-created data type with a fixed set of values
• To create an enumerated data type, use:
– The keyword enum
– An identifier for the type
– A pair of curly braces that contain a list of the enum
constants
enum Month {JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC};

25
26
Creating Enumerations (cont’d.)

27
28
Creating Enumerations (cont’d.)
• You can declare an enumerated type in its own file
– Filename matches the type name and has a .java extension
• Starting with Java 7, you can use comparison
operators with enumeration constants
• You can use enumerations to control a switch
structure

29
30
Creating Enumerations (cont’d.)
• Advantages of creating an enumeration type:
– Only allowed values can be assigned
– Using enums makes the values type-safe
– Provides a form of self-documentation
– You can also add methods and other fields to an enum
type
• Type-safe
– Describes a data type for which only appropriate behaviors
are allowed

31
Summary
• Sorting
– The process of arranging a series of objects in some logical
order
• Two-dimensional arrays
– Both rows and columns
• Arrays class
• ArrayList class
• A programmer-created data type with a fixed set of
values is an enumerated data type

32

You might also like