Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Slide 1
Outline Objectives
Declaration and Initialization array elements Processing arrays Multi-dimensional arrays
Slide 2
Objectives
Applied Given a list of values or objects, write code that creates a onedimensional array that stores those values or objects. Use for loops and enhanced for loops to work with the values or objects in an array. Use the methods of the Arrays class to fill an array, compare two arrays, sort an array, or search an array for a value. Implement the Comparable interface in any class you create. Create a reference to an array and copy elements from one array to another.
Slide 4
Objectives (cont.)
Given a table of values or objects, write code that creates a twodimensional array that stores those values or objects. The array can be either rectangular or jagged. Use for loops and enhanced for loops to work with the values or objects in a two-dimensional array. Given the Java code for an application that uses any of the language elements presented in this chapter, explain what each statement in the application does. Knowledge In general, explain what an array is and how you work with it. Describe the operation of the enhanced for loop and explain why its especially useful with arrays. Explain when you need to implement the Comparable interface in a class you create.
Murachs Java Programming, C11 2011, Mike Murach & Associates, Inc. Slide 5
Objectives (cont.)
Explain what happens when you assign a new array to an existing array variable. Describe the difference between a rectangular array and a jagged array, and explain the difference in how you create them.
Slide 6
Outline Objectives
Declaration and Initialization Motivation Concept of Arrays Declaration Initialization array attributes Processing arrays Multi-dimensional arrays
Slide 7
Motivation, Concept
Motivation:
Use cases: vectors, matrix algebra, indexed collections, e.g., top-10 list Helps implement other collections, e.g., stack, queue, Performance Provide fast access to elements given index, e.g., 3rd, 10th,
Slide 8
Slide 9
Slide 10
Slide 11
Outline Objectives
Declaration and Initialization Array attributes Elements length Processing arrays Multi-dimensional arrays
Slide 12
The syntax for creating an array and assigning values in one statement
type[] arrayName = {value1, value2, value3, ...};
Slide 14
Outline Objectives
Motivation, Concept, Declaration and Initialization array attributes Elements Length Processing arrays for loop java.util.Array Methods: fill, equals, sort, binarySearch, copyOfRange
Passing array to a function/method
Multi-dimensional arrays
Slide 15
Q. Which of the following code-segments throw ArrayIndexOutOfBoundsException? int[] values = new int[10];
(a.) values[10] = 10 ; (b.) values[values.length] = 3; (c.) for (int i=0; i <= values.length; i++) {values[i] = i ;} (d) all of the above
Slide 16
Slide 17
Slide 18
Outline Objectives
Motivation, Concept, Declaration and Initialization array attributes Processing arrays for loop enhanced for loop java.util.Array Methods: fill, equals, sort, binarySearch, copyOfRange
Passing array to a function/method
Multi-dimensional arrays
Slide 19
Slide 20
Q. Which for loop is less likely to throw ArrayIndexOutOfBoundsException? (a.) Enhanced, e.g., for (int v : values ) { s += v ; } (b.) Traditional, e.g., for (int i=0; i < v.length; i++){s += v[i];} Q. Which code-fragment be easily rewritten using enhanced for loop ? (a.) for (int i=0; i < v.length; i++) {v[i] = i ;} (b.) for (int i=0; i < v.length; i++) {sum += v[i];}
Murachs Java Programming, C11 2011, Mike Murach & Associates, Inc. Slide 21
Outline Objectives
Motivation, Concept, Declaration and Initialization array attributes Processing arrays for loop enhanced for loop java.util.Array Methods: fill, equals, sort, binarySearch, copyOfRange
Passing array to a function/method
Multi-dimensional arrays
Slide 22
Slide 23
Slide 24
Slide 25
Slide 26
Leading Question: Can sort() be used for array of user-defined objects? Requirement: Its class should implement java.language.Comparable interface
Slide 27
Slide 28
Slide 29
Outline Objectives
Motivation, Concept, Declaration and Initialization array attributes Processing arrays for loop enhanced for loop java.util.Array Methods: fill, equals, sort, binarySearch, copyOfRange
Passing array to a function/method
Multi-dimensional arrays
Slide 30
Slide 32
Slide 33
5. Since Arrays.sort() can sort String objects, what must be true about the String class? (a.) it has a sort method (b.) it implements comparable interface (c.) it implements comparable method
6. Which of the following code-segments throw ArrayIndexOutOfBoundsException? int[] v = new int[3]; (a.) v[3] = 3 ; (b.) v[v.length] = 3; (c.) for (int i=0; i <= v.length; i++) {v[i] = i;} (d) all three 7. Which of the following are true after given code-fragment? int[] x = {1, 2, 3}; int[] y = {1, 2, 3}; int[] z = a; (a.) (x == y) (b.) Arrays.equals(x, y) (c.) (x == y) (d.) Arrays.equals(x, z)
(e.) (y == z)
8. What is the value of i after the following code fragment? int[] x ={3,6,2}; Arrays.sort(x); int i=Arrays.binarySearch(x, 3); (a.) 0 (b.) 1 (c.) 2 (d.) 3
Slide 35
Outline Objectives
Motivation, Concept, Declaration and Initialization array attributes Processing arrays Multi-dimensional arrays Rectangular Jagged
Slide 36
Slide 37
Slide 38
Slide 39
Outline Objectives
Motivation, Concept, Declaration and Initialization array attributes Processing arrays Multi-dimensional arrays Rectangular Jagged
Slide 40
Slide 41
0 1 2 3 4 5 6 7 8 9
Slide 42
Slide 44