1 / 6

More Arrays

More Arrays. The Partially Filled Array Often create array large enough to handle worst case That means, in practice, array is seldom fully utilized Need extra variable to keep track of used portion (Note that ArrayLists can avoid that problem if used right)

ljake
Download Presentation

More Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Arrays • The Partially Filled Array • Often create array large enough to handle worst case • That means, in practice, array is seldom fully utilized • Need extra variable to keep track of used portion • (Note that ArrayLists can avoid that problem if used right) • Use the proper methods for adding and removing • Results in the size() method being correct

  2. More Arrays • Partially Filled Array Example • Roster of team: may not be filled (assume a Team class) String[] roster = new String[MAX_TEAM_SIZE]; int numMembers = 0; publicvoid addPlayer(String name){ roster[numMembers] = name; numMembers++; } • What was (carelessly) assumed here? publicvoid printTeam(){ for (int k = 0; k < numMembers; k++) System.out.println(roster[k]); } • Why didn’t we use roster.length here?

  3. More Arrays • Partially Filled Array Example (continued) • Removing a team member can be non-trivial publicvoid removePlayer(String name){ int loc = -1; for (int k = 0; k < numMembers; k++){ if (roster[k].equals(name)) { loc = k; break; } } // What if not found: How do we tell? roster[loc] = roster[numMembers]; // what’s this? numMembers--; } • What were some of the (dubious??) assumptions here?

  4. The Arrays Class • Collection of methods used with arrays • See API • Following very useful • static void fill(typearrayName, type value) • Arrays initialized to 0 (or null) on creation • Allows other values or “make sure” • static void sort(typearrayName) • Will look at sorting later • State-of-the-art sorts • Data must be Comparable • static List asList(TypearrayName) • Useful in converting arrays to ArrayLists and other lists

  5. Computing Frequency publicclass FrequencyCounter { final static int MAX_VALUE = 10; final static int SIZE = MAX_VALUE+1; final static String stars = "*******************************"; int[] counters; public FrequencyCounter() { counters = newint[SIZE]; Arrays.fill(counters, 0); } publicvoid getFreq(int[] data){ for (int k = 0; k < data.length; k++){ int n = data[k]; counters[n]++; } }

  6. Computing Frequency public void histogram(){ for (int k = counters.length -1; k >= 0; k--){ int f = counters[k]; System.out.println(k + "\t" + stars.substring(0, f)); } } public static void main(String[] args) { int[] test = {10, 9, 10, 7, 4, 9, 9, 10, 8, 9, 8, 10, 9}; FrequencyCounter fc = new FrequencyCounter(); fc.getFreq(test); fc.histogram(); } }

More Related