Presentation is loading. Please wait.

Presentation is loading. Please wait.

Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?  the rungs of a.

Similar presentations


Presentation on theme: "Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?  the rungs of a."— Presentation transcript:

1 Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?  the rungs of a ladder  the links in a chain  the bricks in the wall of a building  the employees in a company’s organizational chart  the items in an array The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

2 Java Array Review The one-dimensional array (vector) is a basic data structure (container) in many programming languages. Write a declaration of a private instance variable, called weights, that is an array of five double items. Write a declaration of a local variable, called names, that is an array of ten String s. Write an instruction to triple the value of the third cell of weights. Write a loop to output the complete content of names. Show all code needed to declare and assign a local variable, called threeSets, that is an array of the following three sets of String (each bounded by cardinality of five). { “dog”, “cat” } { } { “big”, “little” } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

3 Example Processing items from a container in linear order is called sequential access. int[ ] powersOfTwo; powersOfTwo = new int[10]; powersOfTwo[0] = 1; for (int k = 1; k != powersOfTwo.length; k++) { powersOfTwo[k] = powersOfTwo[k-1] * 2; } Arrays can also be processed via direct access by referring to a particular item without first processing the other items preceding it. Example System.out.println( “The fifth power of 2 is “ + powersOfTwo[5] ); Note: Some linear containers support direct access. Some non-linear containers support sequential access. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub. The Collection for loop can also be used to process sequentially.

4 In Java arrays fit somewhere in between a true reference object and a primitive. Is array behavior more like a reference or a primitive for each of the following?  instantiation (Is new generally required or never used?)  assignment (Does “=“ mean to copy a binding or a value?)  parameter passage (Does passing twoWordCities allow a method to alter the array content or not?)  equals (Does equals work like ==, testing identity?)  Can you override equals() and toString() for your arrays? String[ ] twoWordCities = {“New York”, “Los Angeles”, “La Crosse”};  conformance (Does every array conform to Object?)  Inheritance (Is... extends String[]... possible?) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

5 One Dimensional Array [0][1][2][3][4][5][6][7][8][9] Two Dimensional Array [0][1][2][3][4][5][6][7][8][9] [0] [1] [2] [3] Three Dimensional Array [0][1][2][3][4][5][6][7][8][9] [0] [1] [2] [3] [0] [1] Show a Java declaration for each. Show an instantiation for each. Show an expression for each red cell. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

6 Consider the following objects.  A grocery cart of purchases (a vector of purchases).  A check-out lane (vector of grocery carts).  A grocery store (vector of check-out lanes)  A grocery chain (vector of grocery stores) Do all grocery stores contain the same number of check-out lanes? Are all check-out lanes the same length? Show a Java declaration and instantiation code for a single check-out lane with three carts consisting of space for 8, 6 and 2 purchases. When the length of different parts of an array differ, the array is called _______.

7 Non-linear information, such as a multi-dimensional table, can be perceived in many ways. Example : (Consider the following text.) Different abstract views: The look of sadness overwhelmed the faces of the strong. Young children wept. World leaders spoke of a great societal loss. Twas the day after the last Far Side. 1) a collection of five lines 3) a 2-dimensional square grid of symbols 4) a 1-dimensional vector of lines 2) a collection of four English sentences. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

8 Consider different data structures for representing the following. Option #1: Square 2D array of char The look of sadness overwhelmed the faces of the strong. Young children wept. World leaders spoke of a great societal loss. Twas the day after the last Far Side.  Show a declaration for an object to store this data structure.  Show the code to instantiate the array and assign it the content above.  Show the code to output the 14th character of the second line. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

9 Consider different data structures for representing the following. Option #2: One-dimensional array of char (a linear view) The look of sadness overwhelmed the faces of the strong. Young children wept. World leaders spoke of a great societal loss. Twas the day after the last Far Side.  Show a declaration for an object to store this data structure.  Show the code to instantiate the array and assign it the content above.  Show the code to output the 14th character of the second line. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

10 Consider different data structures for representing the following. Option #3: One-dimensional array of String (a ragged view) The look of sadness overwhelmed the faces of the strong. Young children wept. World leaders spoke of a great societal loss. Twas the day after the last Far Side.  Show a declaration for an object to store this data structure.  Show the code to instantiate the array and assign it the content above.  Show the code to output the 14th character of the second line. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

11 Write the code to output the text for the 2D array of char. The look of sadness overwhelmed the faces of the strong. Young children wept. World leaders spoke of a great societal loss. Twas the day after the last Far Side. How does your code change if printing by column? The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.


Download ppt "Some containers are called linear because their content (items) are stored in a single row (line). Which of the following are linear?  the rungs of a."

Similar presentations


Ads by Google