1 CSC 221: Computer Programming I Spring 2008 ArrayLists and arrays  example: letter frequencies  autoboxing/unboxing  ArrayLists vs. arrays  example:

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Inheritance and efficiency  ArrayList  SortedArrayList  tradeoffs with adding/searching.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 7 – Arrays.
Alice in Action with Java
Building Java Programs
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Writing Methods Using if, loops, and variables to implement algorithms Copyright © 2012 Pearson Education, Inc.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
1 CSC 222: Object-Oriented Programming Spring 2012 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf,
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
1 CSC 222: Object-Oriented Programming Spring 2013 Java interfaces & polymorphism  Comparable interface  defining an interface  implementing an interface.
1 CSC 222: Object-Oriented Programming Spring 2012 Searching and sorting  sequential search  algorithm analysis: big-Oh, rate-of-growth  binary search.
1 CSC 321: Data Structures Fall 2013 HW1 autopsy 1.parallel lists 2.Comparable Word objects 3.Comparators 4.MVC pattern.
1 CSC 222: Object-Oriented Programming Spring 2012 recursion & sorting  recursive algorithms  base case, recursive case  silly examples: fibonacci,
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 CSC 221: Computer Programming I Fall 2004 Lists, data access, and searching  ArrayList class  ArrayList methods: add, get, size, remove  example:
1 CSC 222: Object-Oriented Programming Spring 2013 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf,
1 CSC 221: Computer Programming I Spring 2008 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf,
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
1 CSC 221: Computer Programming I Fall 2004 ArrayLists and OO design  ArrayList summary  example: word frequencies  object-oriented design issues cohesion.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
Arithmetic, Class Variables and Class Methods Week 11
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
The ArrayList Data Structure Standard Arrays at High Speed!
Chapter 9 Introduction to Arrays Fundamentals of Java.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC 427: Data Structures and Algorithm Analysis
CSC 222: Object-Oriented Programming Fall 2017
CSC 222: Object-Oriented Programming Spring 2017
CSC 222: Object-Oriented Programming Spring 2015
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming Fall 2017
CSC 221: Computer Programming I Fall 2005
CSC 221: Computer Programming I Fall 2005
CS 106A, Lecture 19 ArrayLists
Can store many of the same kind of data together
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CSC 221: Computer Programming I Spring 2010
CSC 221: Computer Programming I Fall 2009
Array Lists CSE 1310 – Introduction to Computers and Programming
Can store many of the same kind of data together
CSC 222: Object-Oriented Programming Spring 2013
Review: libraries and packages
Chapter 17 JavaScript Arrays
CSC 222: Object-Oriented Programming Spring 2012
Presentation transcript:

1 CSC 221: Computer Programming I Spring 2008 ArrayLists and arrays  example: letter frequencies  autoboxing/unboxing  ArrayLists vs. arrays  example: word frequencies  parallel lists

2 Letter frequency one common tool for identifying the author of an unknown work is letter frequency  i.e., count how frequently each of the letters is used in the work  analysis has shown that an author will tend to have a consistent pattern of letter usage will need 26 counters, one for each letter  traverse each word and add to the corresponding counter for each character  having a separate variable for each counter is not feasible  instead have an ArrayList of 26 counters this.counts.get(0) is the counter for 'a' this.counts.get(1) is the counter for 'b'... this.counts.get(25) is the counter for 'z' a: 93 ( 9.2%) b: 12 ( 1.2%) c: 28 ( 2.8%) d: 49 ( 4.9%) e: 150 (14.9%) f: 21 ( 2.1%) g: 23 ( 2.3%) h: 65 ( 6.4%) i: 59 ( 5.8%) j: 0 ( 0.0%) k: 2 ( 0.2%) l: 39 ( 3.9%) m: 14 ( 1.4%) n: 71 ( 7.0%) o: 81 ( 8.0%) p: 15 ( 1.5%) q: 1 ( 0.1%) r: 70 ( 6.9%) s: 36 ( 3.6%) t: 109 (10.8%) u: 15 ( 1.5%) v: 20 ( 2.0%) w: 26 ( 2.6%) x: 0 ( 0.0%) y: 10 ( 1.0%) z: 0 ( 0.0%) letter frequencies from the Gettysburg address

3 Letter frequency example initially, have ArrayList of 26 zeros: after processing "Fourscore" : after processing the entire Gettysburg address

4 Autoboxing & unboxing natural assumption: will store the frequency counts in an ArrayList of ints private ArrayList counts; // SORRY, WON'T WORK!  unfortunately, ArrayLists can only store object types (i.e., no primitives)  fortunately, there exists a class named Integer that encapsulates an int value private ArrayList counts;  the Java compiler will automatically convert an int value into an Integer object when you want to store it in an ArrayList (called autoboxing ) convert an Integer value back into an int when need to apply an arithmetic operation on it (called unboxing ) BE CAREFUL: Java will not unbox an Integer for comparison if (this.counts.get(0) == this.counts.get(1)) { … } == will test to see if they are the same Integer objects

5 public class LetterFreq1 { private ArrayList counts; private int numLetters; public LetterFreq1(String fileName) throws java.io.FileNotFoundException { INITIALIZE this.counts AND this.numLetters FOR EACH WORD IN THE FILE FOR EACH CHARACTER IN THE WORD IF THE CHARACTER IS A LETTER DETERMINE ITS POSITION IN THE ALPHABET INCREMENT THE CORRESPONDING COUNT IN this.counts INCREMENT this.numLetters } public int getCount(char ch) { IF ch IS A LETTER DETERMINE ITS POSITION IN THE ALPHABET ACCESS & RETURN THE CORRESPONDING COUNT IN this.counts OTHERWISE RETURN 0 } public double getPercentage(char ch) { IF ch IS A LETTER DETERMINE ITS POSITION IN THE ALPHABET ACCESS THE CORRESPONDING COUNT IN this.counts CALCULATE & RETURN THE PERCENTAGE OTHERWISE RETURN 0.0 } public void showCounts() { FOR EACH LETTER IN THE ALPHABET DISPLAY THE LETTER, ITS COUNT & PERCENTAGE } LetterFreq1 design

6 LetterFreq1 implementation import java.util.ArrayList; import java.util.Scanner; import java.io.File; public class LetterFreq1 { private static final String LETTERS = "abcdefghijklmnopqrstuvwxyz"; private ArrayList counts; private int numLetters; public LetterFreq1(String fileName) throws java.io.FileNotFoundException { this.counts = new ArrayList (); for (int i = 0; i < LetterFreq1.LETTERS.length(); i++) { this.counts.add(0); } this.numLetters = 0; Scanner infile = new Scanner(new File(fileName)); while (infile.hasNext()) { String nextWord = infile.next(); for (int c = 0; c < nextWord.length(); c++) { char ch = nextWord.charAt(c); if (Character.isLetter(ch)) { int index = LetterFreq1.LETTERS.indexOf(Character.toLowerCase(ch)); this.counts.set(index, this.counts.get(index)+1); this.numLetters++; }... will use a constant to store the alphabet initialize the letter counts for each word, process each letter … … get letter’s index, increment its count

7 LetterFreq1 implementation (cont.)... public int getCount(char ch) { if (Character.isLetter(ch)) { int index = LetterFreq1.LETTERS.indexOf(Character.toLowerCase(ch)); return this.counts.get(index); } else { return 0; } public double getPercentage(char ch) { if (Character.isLetter(ch) && this.numLetters > 0) { int index = LetterFreq1.LETTERS.indexOf(Character.toLowerCase(ch)); return Math.round(1000.0*this.counts.get(index)/this.numLetters)/10.0; } else { return 0.0; } public void showCounts() { for (int i = 0; i < LetterFreq1.LETTERS.length(); i++) { char ch = LetterFreq1.LETTERS.charAt(i); System.out.println(ch + ": " + this.getCount(ch) + "\t(" + this.getPercentage(ch) + "%)"); } if it is a letter, access & return its count if it is a letter, calculate & return its percentage display all letters, counts and percentages

8 Interesting comparisons a: 93 ( 9.2%) b: 12 ( 1.2%) c: 28 ( 2.8%) d: 49 ( 4.9%) e: 150 (14.9%) f: 21 ( 2.1%) g: 23 ( 2.3%) h: 65 ( 6.4%) i: 59 ( 5.8%) j: 0 ( 0.0%) k: 2 ( 0.2%) l: 39 ( 3.9%) m: 14 ( 1.4%) n: 71 ( 7.0%) o: 81 ( 8.0%) p: 15 ( 1.5%) q: 1 ( 0.1%) r: 70 ( 6.9%) s: 36 ( 3.6%) t: 109 (10.8%) u: 15 ( 1.5%) v: 20 ( 2.0%) w: 26 ( 2.6%) x: 0 ( 0.0%) y: 10 ( 1.0%) z: 0 ( 0.0%) letter frequencies from the Gettysburg address a: 8791 ( 8.2%) b: 1475 ( 1.4%) c: 2398 ( 2.2%) d: 4930 ( 4.6%) e: (12.6%) f: 2000 ( 1.9%) g: 2531 ( 2.4%) h: 7373 ( 6.8%) i: 7510 ( 7.0%) j: 146 ( 0.1%) k: 1158 ( 1.1%) l: 4713 ( 4.4%) m: 2104 ( 2.0%) n: 7013 ( 6.5%) o: 8145 ( 7.6%) p: 1524 ( 1.4%) q: 209 ( 0.2%) r: 5437 ( 5.0%) s: 6500 ( 6.0%) t: ( 9.9%) u: 3465 ( 3.2%) v: 846 ( 0.8%) w: 2675 ( 2.5%) x: 148 ( 0.1%) y: 2262 ( 2.1%) z: 78 ( 0.1%) letter frequencies from Alice in Wonderland a: ( 7.6%) b: 1956 ( 1.4%) c: 5272 ( 3.7%) d: 4392 ( 3.1%) e: (12.9%) f: 4228 ( 2.9%) g: 2114 ( 1.5%) h: 7607 ( 5.3%) i: ( 8.3%) j: 106 ( 0.1%) k: 568 ( 0.4%) l: 5697 ( 4.0%) m: 3253 ( 2.3%) n: 9983 ( 6.9%) o: ( 7.8%) p: 2678 ( 1.9%) q: 344 ( 0.2%) r: 8337 ( 5.8%) s: 8982 ( 6.2%) t: (10.5%) u: 3394 ( 2.4%) v: 1737 ( 1.2%) w: 2506 ( 1.7%) x: 537 ( 0.4%) y: 2446 ( 1.7%) z: 115 ( 0.1%) letter frequencies from Theory of Relativity

9 ArrayLists and arrays ArrayList enables storing a collection of objects under one name  can easily access and update items using get and set  can easily add and remove items, and shifting occurs automatically  can pass the collection to a method as a single object ArrayList is built on top of a more fundamental Java data structure: the array  an array is a contiguous, homogeneous collection of items, accessible via an index  arrays are much less flexible than ArrayLists e.g.,the size of an array is fixed at creation, so you can't add items indefinitely when you add/remove from the middle, it is up to you to shift items so, why use arrays? 1 st answer: DON'T! when possible, take advantage of ArrayList's flexibility 2 nd answer: arrays can store primitives directly, without autoboxing/unboxing 3 rd answer: initializing/accessing an array can be easier in some circumstances

10 Arrays to declare an array, designate the type of value stored followed by [] String[] words;int[] counters; to create an array, must use new (an array is an object)  specify the type and size inside brackets following new words = new String[100];counters = new int[26];  or, if you know what the initial contents of the array should be, use shorthand: int[] years = {2001, 2002, 2003, 2004, 2005}; to access or assign an item in an array, use brackets with the desired index  similar to the get and set methods of ArrayList String str = word[0]; // note: index starts at 0 // (similar to ArrayLists) for (int i = 0; i < 26, i++) { counters[i] = 0; }

11 LetterFreq2 implementation import java.util.Scanner; import java.io.File; public class LetterFreq2 { private static final String LETTERS = "abcdefghijklmnopqrstuvwxyz"; private int[] counts; private int numLetters; public LetterFreq2(String fileName) throws java.io.FileNotFoundException { this.counts = new int[LetterFreq1.LETTERS.length()]; for (int i = 0; i < LetterFreq1.LETTERS.length(); i++) { this.counts[i] = 0; } this.numLetters = 0; Scanner infile = new Scanner(new File(fileName)); while (infile.hasNext()) { String nextWord = infile.next(); for (int c = 0; c < nextWord.length(); c++) { char ch = nextWord.charAt(c); if (Character.isLetter(ch)) { int index = LetterFreq1.LETTERS.indexOf(Character.toLowerCase(ch)); this.counts[index]++; this.numLetters++; }... could instead make the field an array access/assign an entry using [ ] increment is simpler (no need to get then set) initialize array to desired size

12 LetterFreq2 implementation (cont.)... public int getCount(char ch) { if (Character.isLetter(ch)) { int index = LetterFreq1.LETTERS.indexOf(Character.toLowerCase(ch)); return this.counts[index]; } else { return 0; } public double getPercentage(char ch) { if (Character.isLetter(ch) && this.numLetters > 0) { int index = LetterFreq1.LETTERS.indexOf(Character.toLowerCase(ch)); return Math.round(1000.0*this.counts[index]/this.numLetters)/10.0; } else { return 0.0; } public void showCounts() { for (int i = 0; i < LetterFreq1.LETTERS.length(); i++) { char ch = LetterFreq1.LETTERS.charAt(i); System.out.println(ch + ": " + this.getCount(ch) + "\t(" + this.getPercentage(ch) + "%)"); } other method essentially the same (array access uses [ ] instead of the get method for ArrayLists)

13 Why arrays? general rule: ArrayLists are better, more abstract, arrays – USE THEM!  they provide the basic array structure with many useful methods provided for free get, set, add, size, contains, indexOf, remove, …  plus, the size of an ArrayList automatically adjusts as you add/remove items when might you want to use an array?  if the size of the list will never change and you merely want to access/assign items, then the advantages of arrays may be sufficient to warrant their use if the initial contents are known, they can be assigned when the array is created String[] answers = { "yes", "no", "maybe" }; the [] notation allows for both access and assignment (instead of get & set) int[] counts = new int[11];... counts[die1.roll() + die2.roll()]++; you can store primitive types directly, so no autoboxing/unboxing

14 Related example: word frequencies recall:  Dictionary class reads in words from a file, stores unique words  LetterFreq class reads in words from a file, stores letter frequencies could combine ideas from these two classes to perform a different task: keep frequency counts for individual words fourscore: 1 and: 5 seven: 1 years: 1 ago: 1 our: 2 fathers: 1 brought: 1 forth: 1 on: 2 this: 3. as in Dictionary, will need an ArrayList to store the words as they are read in as in LetterFreq, will need to keep a counter for each word that is stored: first time read, create new entry with count of 1 each subsequent time, just increment the count

15 Parallel lists one approach to storing the words and their associated counts is with parallel lists  related values are stored in separate lists, with corresponding values at corresponding indices e.g., words stores the words counts stores the frequency counts for those words note: counts.get(i) is the number of times that words.get(i) appears in the file "forescore""and""seven" 152… words counts …

16 WordFreq implementation public class WordFreq { private ArrayList words; private ArrayList counts; public WordFreq() { this.words = new ArrayList (); this.counts = new ArrayList (); } public WordFreq(String fileName) throws java.io.FileNotFoundException { this.words = new ArrayList (); this.counts = new ArrayList (); Scanner infile = new Scanner(new File(fileName)); while (infile.hasNext()) { String nextWord = infile.next(); this.addWord(nextWord); } public void addWord(String newWord) { int index = words.indexOf(newWord); if (index == -1) { this.words.add(newWord); this.counts.add(1); } else { this.counts.set(index, this.counts.get(index)+1); }... must use an ArrayList for the counts since the size will grow initialize both lists to be empty call addWord to add each word as it is read in if not already stored, add the word with count of 1, else increment its count

17 WordFreq implementation (cont.)... public int wordCount(String desiredWord) { int index = this.words.indexOf(desiredWord); if (index == -1) { return 0; } else { return this.counts.get(index); } public int numWords() { return this.words.size(); } public void showAll() { for (int i = 0; i < this.words.size(); i++) { System.out.println(this.words.get(i) + ": " + this.counts.get(i)); } wordCount looks up the word, and if found returns its count numWords simply returns the size of the lists showAll traverses the lists and shows each word & count