Chapter 9: Advanced Array Concepts

Slides:



Advertisements
Similar presentations
Understanding the Need for Sorting Records
Advertisements

Chapter 9: Advanced Array Manipulation
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual C++ Programming: Concepts and Projects
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
11.5 SORTING ARRAY Sorting is the process of transforming a list into an equivalent list, in which the elements are arranged in ascending or descending.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Alice in Action with Java
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 9: Arrays and Strings
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Programming Logic and Design Fourth Edition, Comprehensive
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
Chapter 8 Arrays and Strings
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Chapter 11 Arrays Continued
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 8: Arrays.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 12 Manipulating Larger Quantities of Data.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
Arrays Chapter 7.
Chapter 6: Using Arrays.
Arrays 2.
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 8 Slides from GaddisText
Review of Arrays and Pointers
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Arrays.
Presentation transcript:

Chapter 9: 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 Java Programming, Seventh Edition

Sorting Array Elements Using the Bubble Sort Algorithm 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 Java Programming, Seventh Edition

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 Java Programming, Seventh Edition

Using the Bubble Sort Algorithm 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 Java Programming, Seventh Edition

Using the Bubble Sort Algorithm (cont’d.) Java Programming, Seventh Edition

Using the Bubble Sort Algorithm (cont’d.) Java Programming, Seventh Edition

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 Java Programming, Seventh Edition

Sorting Array Elements Using the Insertion Sort Algorithm 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 Java Programming, Seventh Edition

Sorting Array Elements Using the Insertion Sort Algorithm (cont’d.) Figure 9-6 The insertion sort Java Programming, Seventh Edition

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]; Java Programming, Seventh Edition

Using Two-Dimensional and Other Multidimensional Arrays (cont’d.) Figure 9-9 View of a two-dimensional array in memory Java Programming, Seventh Edition

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

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) Java Programming, Seventh Edition

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 Java Programming, Seventh Edition

Understanding Ragged Arrays 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 Java Programming, Seventh Edition

Using Other Multidimensional Arrays Arrays with more than one dimension Create arrays of any size Keep track of the order of variables needed as subscripts Don’t exhaust your computer’s memory Java Programming, Seventh Edition

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 Java Programming, Seventh Edition

Using the Arrays Class (cont’d.) Java Programming, Seventh Edition

Figure 9-15 The ArraysDemo application Java Programming, Seventh Edition

Figure 9-17 The VerifyCode application Java Programming, Seventh Edition

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 Java Programming, Seventh Edition

Using the ArrayList Class (cont’d.) Java Programming, Seventh Edition

Using the ArrayList Class (cont’d.) Figure 9-20 The ArrayListDemo program Java Programming, Seventh Edition

Understanding the Limitations of the ArrayList Class You cannot use an ArrayList to store primitive types You must cast elements to the appropriate reference type, or you must declare a reference type in the ArrayList declaration Java Programming, Seventh Edition

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}; Java Programming, Seventh Edition

Java Programming, Seventh Edition

Creating Enumerations (cont’d.) Java Programming, Seventh Edition

Figure 9-24 The EnumDemo class Java Programming, Seventh Edition

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 Java Programming, Seventh Edition

Figure 9-26 The EnumDemo2 class Java Programming, Seventh Edition

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 Java Programming, Seventh Edition

You Do It Using a Bubble Sort Using an Insertion Sort Using a Two-Dimensional Array Using Arrays Class Methods Creating Enumerations Java Programming, Seventh Edition

Don’t Do It Don’t forget that the first subscript used with a two-dimensional array represents the row, and that the second subscript represents the column Don’t try to store primitive data types in an ArrayList structure Don’t think enum constants are strings; they are not enclosed in quotes Java Programming, Seventh Edition

Summary Sorting Two-dimensional arrays Arrays class ArrayList class 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 Java Programming, Seventh Edition