Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Linear Containers 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

2 Single Dimensional Arrays
The one-dimensional array (vector) is a basic data structure (container) in many programming languages. Java Array Review 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 Strings. 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” }

3 Sequential or Direct Access?
Processing items from a container in linear order is called sequential access. Example int[ ] powersOfTwo; powersOfTwo = new int[10]; powersOfTwo[0] = 1; for (int k = 1; k != powersOfTwo.length; k++) { powersOfTwo[k] = powersOfTwo[k-1] * 2; } The Collection for loop can also be used to process sequentially. 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.

4 Are Arrays Primitive or Reference?
In Java arrays fit somewhere in between a true reference object and a primitive. String[ ] twoWordCities = {“New York”, “Los Angeles”, “La Crosse”}; 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?)  conformance (Does every array conform to Object?)  Can you override equals() and toString() for your arrays?  Inheritance (Is ... extends String[] ... possible?)

5 The Need for More Dimensions
One Dimensional Array [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Show a Java declaration for each. Two Dimensional Array [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Show an instantiation for each. Show an expression for each red cell. Three Dimensional Array [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

6 Example: More Dimensions
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? (see next slide)

7 Slices A multi-dimensional array is really a one-dimensional array of arrays (slices). [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Show an expression for one slice of a 2-dimensional array. 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 _______.

8 Abstraction & Multiple Dimensions
Non-linear information, such as a multi-dimensional table, can be perceived in many ways. Example: (Consider the following text.) 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. Different abstract views: 1) a collection of five lines 2) a collection of four English sentences. 3) a 2-dimensional square grid of symbols 4) a 1-dimensional vector of lines

9 Implementation Decisions
Consider different data structures for representing the following. 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. Option #1: Square 2D array of char  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.

10 Implementation Decisions
Consider different data structures for representing the following. 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. Option #2: One-dimensional array of char (a linear view)  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.

11 Implementation Decisions
Consider different data structures for representing the following. 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. Option #3: One-dimensional array of String (a ragged view)  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.

12 Loops for Processing Arrays
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. Write the code to output the text for the 2D array of char. How does your code change if printing by column?


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

Similar presentations


Ads by Google