Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.

Similar presentations


Presentation on theme: "1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS."— Presentation transcript:

1 1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS

2 2 OBJECTIVES! Create a method that traverses an array Reverse an array

3 3 PRINTING AN ARRAY… If the values are in an array, however, Java provides a much easier way… what is it? (This is actually from the very end of 7.1.) int[] myArray = {8, 42, 13, 77, 8, 16}; System.out.println(Arrays.toString(myArray));

4 4 ARRAY TRAVERSAL We’ve already discussed using for and for-each loops to walk through the values in an array. As you do this you can test each element and/or perform an action on that element. We traversed a list of word lengths and counted the number of them greater than the average length. (At least, that was the goal!)

5 5 ARRAY TRAVERSAL When traversing an array using a for loop (not a for-each loop!), you can modify the array by changing the values stored at a given index. How would you create a method to double all of the elements in an int array (int[])? public static void doubleIt(int[] array) { for (int i = 0; i < array.length; i++) { array[i] *= 2; // or array[i] = array[i] * 2; } }

6 6 SWAPPING There are several algorithms that can take advantage of swapping two elements in an array. What are they? Reversing and Sorting both require swapping elements. How would you create a method to swap any two elements (given their indexes) in a double array (double[])? public static void swap(double[] array, int i, int j) { double temp = array[i]; array[i] = array[j]; array[j] = temp; }

7 7 SWAPPING / REVERSING Now that we have a helper method to swap any two element values, we can write methods to do more complex changes. How would you create a method to reverse the elements in a double array (double[])? public static void reverse(double[] array) { for (int i = 0; i < array.length / 2; i++) { int j = array.length – 1 – i; swap(array, i, j); } }

8 8 LET’S TRY IT! Change the average word length program to 1. Print the complete array of words using Arrays.toString(). 2. Create a new array that contains just the long words. 3. Print the array of long words. 4. Write a method to reverse a String array (String[]) in place. 5. Reverse the complete and long word arrays and print them out. This sounds like a lot, but it’s really not, and we just went over everything you need for reversing!

9 9 LET’S TRY IT! Before you start… what approach would you use? How many words? 5 Word #1? ice Word #2? cream Word #3? social Word #4? sprinkles Word #5? chocolate Average word length: 6.4 Number of words longer than average: 2 All Words : [ice, cream, social, chocolate, sprinkles] Long Words: [chocolate, sprinkles] Reversed Words: [sprinkles, chocolate, social, cream, ice] Reversed Long : [sprinkles, chocolate]

10 10 WHAT WE COVERED You should be able to create a method that properly traverses an array You should be able reverse an array


Download ppt "1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS."

Similar presentations


Ads by Google