Lecture9

# Lecture9 - ArrayList exercise: finding the most frequent...

This preview shows pages 1–8. Sign up to view the full content.

1 ArrayList exercise: finding the most frequent element in an array Write a program that finds the most frequently occurring element in an array of ints. Use ArrayLists to count the number of occurrences of each element. At the end print the element and its number of occurrences. public Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element. public int indexOf( Object elem) Searches for the first occurence of the given argument. Returns: the index of the first occurrence of the argument in this list; returns -1 if the object is not found.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 import java.util.ArrayList; public class MostFrequentElement { /** Assume that the given array contains at least one element. **/ public static void main(String [] args) { // 1 is the most frequent element, it appears 7 times: int[] elements = {1, 3, 4, 1, 5, 2, 3, 6, 6, 6, 4, 1, 2, 6, 2, 3, 1, 2, 1, 5, 5, 1, 1, 5, 4}; // Your code goes // fill in the appropriate results: System.out.println("The most frequent element " + " occurs " + " times"); } }
3 A solution: uniqueElements import java.util.ArrayList; counts public class MostFrequentElement { public static void main(String [] args) { int [] elements = {1, 3, 4, 1, 5, 2, 3, 6, 6, 6, 4, 1, 2, 6, 2, 3, 1, 2, 1, 5, 5, 1, 1, 5, 4}; // create ArrayList of elements and ArrayList of their counts ArrayList<Integer> uniqueElements = new ArrayList<Integer>(); ArrayList<Integer> counts = new ArrayList<Integer>(); // traverse the array, collect the elements/counts for (int element: elements) { int index = uniqueElements.indexOf(element); // element has been seen: if (index != -1 ) { // increment its count int newCount = counts.get(index) + 1; counts.set(index, newCount); } else { // add it to uniqueElements, set its count to 1 uniqueElements.add(element); counts.add(1); 1 3 4 5 1 1 1 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 } } System.out.println(uniqueElements); System.out.println(counts); // find maximum count, get the corresponding element int maxCount = 0; int index = -1; for(int i = 0; i < counts.size(); i++) { if (maxCount < counts.get(i)) { maxCount = counts.get(i); index = i; } } // fill in the appropriate results: System.out.println("The most frequent element " + uniqueElements.get(index) + " occurs " + maxCount + " times"); } }
5 Chapter 6: Writing a Program Developing Classes List the instance variables and methods. Figure out what the class is supposed to do . Implement the class. Write test code for the methods.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Example: Simple Dot Com Game Goal : sink a Dot Com Setup : A Dot Com takes up three cells on a virtual 1X7 grid. How to Play: Computer prompts you to enter a guess (an integer 2, 4, etc.), You will see a result: “Hit”, “miss” or “kill”. 0 1 2 3 4 5 6 AskMe.com
7 an int array to hold the location cells. an int to hold the number of hits checkYourself(): takes a string for the user’s guess (“1”, “3”). Check it and returns a result representing a “hit”, “miss”, or “kill”. SetLocationCells(): takes an int array (which has the three cell location as ints) Game starts. C reate one dot com and gives it a location on three cells in a single row of seven cells.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## Lecture9 - ArrayList exercise: finding the most frequent...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online