Arrays Chapter 6 1 Reminders Project 4 released



![Creating and Accessing Arrays • example double[] temperature = new double[7]; is like declaring Creating and Accessing Arrays • example double[] temperature = new double[7]; is like declaring](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-4.jpg)














![Swapping Elements • To swap two elements a[i] and a[j], one of them must Swapping Elements • To swap two elements a[i] and a[j], one of them must](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-19.jpg)





![Multidimensional-Array Basics • example declaration int[][] table = new int [10][6]; or int[][] table; Multidimensional-Array Basics • example declaration int[][] table = new int [10][6]; or int[][] table;](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-25.jpg)
![Multidimensional-Array Basics, cont. • syntax Base_Type[]…[] Array_Name = new Base_Type[Length_1]…[Length_n]; • examples char[][] page Multidimensional-Array Basics, cont. • syntax Base_Type[]…[] Array_Name = new Base_Type[Length_1]…[Length_n]; • examples char[][] page](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-26.jpg)








![Ragged Arrays, cont. • Example int[][] b = new int[3][]; b[0] = new int[5]; Ragged Arrays, cont. • Example int[][] b = new int[3][]; b[0] = new int[5];](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-35.jpg)













![Method draw. Polygon • “syntax” canvas. draw. Polygon(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[] Method draw. Polygon • “syntax” canvas. draw. Polygon(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[]](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-49.jpg)
![Method fill. Polygon • “syntax” canvas. fill. Polygon(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[] Method fill. Polygon • “syntax” canvas. fill. Polygon(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[]](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-50.jpg)
![Method draw. Polyline • “syntax” canvas. draw. Polyline(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[] Method draw. Polyline • “syntax” canvas. draw. Polyline(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[]](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-51.jpg)



- Slides: 54

Arrays Chapter 6 1

Reminders • Project 4 released: due Oct 6 @ 10: 30 pm • Project 1 regrades due by midnight tonight • Project 2 grades posted on Web. CT – regrade requests due by Friday Oct 7 midnight (send to rnehme@cs. purdue. edu) Chapter 6 2

Exam 1 Info • • • Exam will be returned in recitation today Solution available on the website Scores are available on Web. CT Problems/scoring errors: talk to your GTA Exam 1 Stats: – Mean: 73. 2 – Median: 77 – High: 98 Chapter 6 3
![Creating and Accessing Arrays example double temperature new double7 is like declaring Creating and Accessing Arrays • example double[] temperature = new double[7]; is like declaring](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-4.jpg)
Creating and Accessing Arrays • example double[] temperature = new double[7]; is like declaring seven variables of type double, named temperature[0], temperature[1], temperature[2], temperature[3], temperature[4], temperature[5], temperature[6]. • Similar to: Automobile my. Car = new Automobile(“Toyota”); Chapter 6 4

Creating and Accessing Arrays, cont. • These variables can be used just like any other variables of type double. • examples temperature[3] = 32. 0; temperature[6] = temperature[3] + 5; System. out. println(temperature[6]); temperature[index] = 66. 5; • These variables are called indexed variables, elements, or subscripted variables. Chapter 6 5

The length Instance Variable • An array has only one public instance variable, length. • The length variable stores the number of elements the array can hold. • Using Array_Name. length typically produces clearer code than using an integer literal. • length is an instance variable, not a method Array_Name. length() wont work Chapter 6 6

Chapter 6 7

Indices and length • The indices of an array start with 0 and end with Array_Name. length-1. • When a for loop is used to step through an array, the loop control variable should start at 0 and end at length-1. • example for (lcv = 0; lcv < temperature. length; lcv++) Chapter 6 8

Array Index Out of Bounds • Every index must evaluate to an integer which is not less than 0 and not greater than Array_Name. length-1. • Otherwise, the index is said to be out of bounds or invalid. • An out-of-bounds index will produce a runtime error. Chapter 6 9

Arrays of Objects Chapter 6 10

Entire Arrays as Method Arguments • An entire array can be used as a single argument passed to a method. • example double[] a = new double[10]; Sample. Class. change(a); . . . public static void change(double[] d) – No brackets accompany the argument. – Method change accepts an array of any size. Chapter 6 11

Methods that Return Arrays • A method can return an array. • The mechanism is basically the same as for any other returned type. • example public static Base_Type[] Method_Name (Parameter_List) Base_Type Array_Name; … return Array_Name; • The method need not be Chapter 6 public or static. 12

Chapter 6 13

Sorting Arrays • Sometime we want numbers in an array sorted from smallest to largest, or from largest to smallest. • Sometimes we want the strings referenced by an array to be in alphabetical order. • Sorting techniques typically are easy to adapt to sort any type that can be ordered. Chapter 6 14

Selection Sort • The selection sort arranges the values in an an array so that a[0] <= a[1] <= a[2] … <= a[a. length-1] • The selection sort places the smallest item in a[0], the next smallest item in a[1], and so on for all but the last item. for(i = 0; i <a. length-1; i++) place the ith smallest item in a[i] Chapter 6 15

Selection Sort, cont. • Selection sort begins by finding the smallest item in the array and swapping it with the item in a[0]. • Selection sort continues by finding the smallest item in the remainder of the array and swapping it with the next item in array a. • Selection sort terminates when only one item remains. Chapter 6 16

Selection Sort, cont. Chapter 6 17

Chapter 6 18
![Swapping Elements To swap two elements ai and aj one of them must Swapping Elements • To swap two elements a[i] and a[j], one of them must](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-19.jpg)
Swapping Elements • To swap two elements a[i] and a[j], one of them must be saved temporarily. Chapter 6 19

Swapping Elements, cont. • class interchange Chapter 6 20

Introduction to Multidimensional Arrays • An array with more than one index sometimes is useful. • example: savings account balances at various interest rates for various numbers of years – columns for interest rates – rows for years • This two-dimensional table calls for a twodimensional array. Chapter 6 21

Introduction to Multidimensional Arrays, cont. Chapter 6 22

Introduction to Multidimensional Arrays, cont. Chapter 6 23

Introduction to Multidimensional Arrays, cont. • An array with n indices is called an ndimensional array. • The arrays we considered previously, which had one index, were one-dimensional arrays. Chapter 6 24
![MultidimensionalArray Basics example declaration int table new int 106 or int table Multidimensional-Array Basics • example declaration int[][] table = new int [10][6]; or int[][] table;](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-25.jpg)
Multidimensional-Array Basics • example declaration int[][] table = new int [10][6]; or int[][] table; table = new int[10][6]; • The number of bracket pairs in the declaration is the same as the number of indices. Chapter 6 25
![MultidimensionalArray Basics cont syntax BaseType ArrayName new BaseTypeLength1Lengthn examples char page Multidimensional-Array Basics, cont. • syntax Base_Type[]…[] Array_Name = new Base_Type[Length_1]…[Length_n]; • examples char[][] page](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-26.jpg)
Multidimensional-Array Basics, cont. • syntax Base_Type[]…[] Array_Name = new Base_Type[Length_1]…[Length_n]; • examples char[][] page = new char [100][80]; double[][][] three. DPicture = new double[10][20][30]; Some. Class[][] entry = new Some. Class[100][80]; Chapter 6 26

Multidimensional-Array Basics, cont. • Nested for loops can be used to change the values of the elements of the array and to display the values of the elements of the array. Chapter 6 27

Chapter 6 28

Multidimensional-Array Parameters • Methods may have multidimensional-array parameters. Chapter 6 29

Chapter 6 30

Multidimensional-Array Returned Values • A method may return a multidimensional array. • example public static double[][] corner(double[][] s. Array, int i) { double[][] temp = new double[i][i]; … return temp; } Chapter 6 31

Implementation of Multidimensional Arrays • Multidimensional arrays are implemented in Java using one-dimensional arrays. • Consider int[][] table = new int[10][6]; – The array table is a one-dimensional array of length 10. – Its base type is int[]. – Therefore, it is an array of arrays. Chapter 6 32

Implementation of Multidimensional Arrays, cont. • This permits us to use the length instance variable, instead of an integer literal, to control a for loop used to initialize or print the values of the elements of an array. • example for (r = 0; r < table. length; r++) for (c = 0; c < table[r]. length; c++) Chapter 6 33

Ragged Arrays • Since a two-dimensional array in Java is an array of arrays, each row can have a different number of elements (columns). • Arrays in which rows have different numbers of elements are called ragged arrays. Chapter 6 34
![Ragged Arrays cont Example int b new int3 b0 new int5 Ragged Arrays, cont. • Example int[][] b = new int[3][]; b[0] = new int[5];](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-35.jpg)
Ragged Arrays, cont. • Example int[][] b = new int[3][]; b[0] = new int[5]; b[1] = new int[7]; b[2] = new int[4]; Chapter 6 35

(optional) Graphics Supplement: Outline • Part 1: Text Areas and Text Fields • Part 2: Drawing Polygons and Polylines Chapter 6 36

Text Areas and Text Fields • A text area is a window that can be used for text input and text output. – any number of lines – any number of characters per line • A text field allows only one line of characters. • Text area and text fields provide areas for changeable text in a GUI. Chapter 6 37

Programming Example: A Question and Answer Applet • The applet accepts a question, requests some advice, and provides an answer. • The applet contains some advice for the first user, but then takes the advice provided by one user, and uses it to answer the next user. Chapter 6 38

Chapter 6 39

Programming Example: , cont. Chapter 6 40

JText. Area Objects • A text area is an object of the class JText. Area. • A JText. Area can be created with private JText. Area the. Text; … the. Text = new JText. Area(LINES, CHAR_PER_LINE); • Typically, this occurs inside the init method. Chapter 6 41

JText. Area Objects, cont. • The text in the text area can be read using String question = the. Text. get. Text(); • The text in the text area can be set using the. Text. set. Text(“That’s difficult. n” + “I need some advice. n” + “Give me some advice and click. ”); – Text can be entered in excess of the specified size, but may not be entirely visible. Chapter 6 42

JText. Field Objects • A text field is an object of the class JText. Field. • A JText. Field can be created with private JText. Field the. Text; … the. Text = new JText. Field(NUMBER_OF_CHARS); • Typically, this occurs inside the init method. Chapter 6 43

JText. Field Objects, cont. • The text in the text field can be read using String question = the. Text. get. Text(); • The text in the text area can be set using the. Text. set. Text(“That’s all, folks. ”); – Text can be entered in excess of the specified size, but may not be entirely visible. Chapter 6 44

Drawing Polygons and Polylines • The methods draw. Polygon and fill. Polygon allow you to draw polygons which are closed figures made of of line segments that meet at vertices but do not cross. • A polyline is similar to a polygon, but uses the method draw. Polyline and need not be closed. Chapter 6 45

Drawing Polygons and Polylines, cont. Chapter 6 46

Drawing Polygons and Polylines, cont. • class House Chapter 6 47

Drawing Polygons and Polylines, cont. Chapter 6 48
![Method draw Polygon syntax canvas draw PolygonArrayofxs Arrayofys NumberofPoints example private int Method draw. Polygon • “syntax” canvas. draw. Polygon(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[]](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-49.jpg)
Method draw. Polygon • “syntax” canvas. draw. Polygon(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[] x. Coord = {150, 200, 250}; private int[] y. Coord = {100, 40, 20, 40, 100}; … canvas. draw. Polygon(x. Coord, y. Coord, x. Coord. length); Chapter 6 49
![Method fill Polygon syntax canvas fill PolygonArrayofxs Arrayofys NumberofPoints example private int Method fill. Polygon • “syntax” canvas. fill. Polygon(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[]](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-50.jpg)
Method fill. Polygon • “syntax” canvas. fill. Polygon(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[] x. Coord = {150, 200, 250}; private int[] y. Coord = {100, 40, 20, 40, 100}; … canvas. fill. Polygon(x. Coord, y. Coord, x. Coord. length); Chapter 6 50
![Method draw Polyline syntax canvas draw PolylineArrayofxs Arrayofys NumberofPoints example private int Method draw. Polyline • “syntax” canvas. draw. Polyline(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[]](https://slidetodoc.com/presentation_image_h2/020abcfce252a2b38e9d9d5700560ea7/image-51.jpg)
Method draw. Polyline • “syntax” canvas. draw. Polyline(Array_of_xs, Array_of_ys, Number_of_Points); • example private int[] x. Coord = {150, 200, 250}; private int[] y. Coord = {100, 40, 20, 40, 100}; … canvas. draw. Polyline(x. Coord, y. Coord, x. Coord. length); Chapter 6 51

Method draw. Polyline, cont. • There is no automatic line segment from the last point to the first point. • As a consequence, the figure typically is not closed. • A polyline cannot be filled. Chapter 6 52

Summary • You have learned about arrays and how to use them in Java programs. • You have learned how to use array parameters and how to define methods that return an array. • You have learned the proper use of an array as an instance variable. • You have learned about multidimensional arrays. Chapter 6 53

Summary, cont. • (optional) You have learned about text fields and text areas in applets. • (optional) You have learned to draw arbitrary polygons and polylines in applets. Chapter 6 54