










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A solution that uses an array: String[] allWords = new String[1000]; int wordCount = 0;. Scanner input = new Scanner(new File(words.txt)); while (input.
Typology: Schemes and Mind Maps
1 / 18
This page cannot be seen from the preview
Don't miss anything!











Words exercise
Words exercise
String[] allWords = new String[ 1000 ]; int wordCount = 0; Scanner input = new Scanner(new File("words.txt")); while (input.hasNext()) { String word = input.next(); allWords[wordCount] = word; wordCount++; }
Recall: Arrays (7.1)
element : One value in an array. index : 0-based integer to access an element from an array. length : Number of elements in the array.
element 0 element 4 element 9
List Abstraction
[hello, ABC, goodbye, okay] The list object keeps track of the element values that have been added to it, their order, indexes, and its total size. You can add, remove, get, set, ... any index at any time.
Collections
the objects stored are called elements some collections maintain an ordering; some allow duplicates typical operations: add , remove , clear , contains (search), size examples found in the Java class libraries: (covered in this course!) ArrayList, LinkedList, HashMap, TreeSet, PriorityQueue all collections are in the java.util package import java.util.*;
Type parameters (generics)
This is called a type parameter ; ArrayList is a generic class. Allows the ArrayList class to store lists of different types.
ArrayList
ArrayList methods (10.1)*
ArrayList vs. array String[] names = new String[5]; // construct names[0] = "Jessica"; // store String s = names[0]; // retrieve for (int i = 0; i < names.length; i++) { if (names[i].startsWith("B")) { ... } } // iterate ArrayList
ArrayList as param/return public static void name (ArrayList< Type > name ) {// param public static ArrayList< Type > name ( params ) // return
// Returns count of plural words in the given list. public static int countPlural( ArrayList
Exercise solution (partial) ArrayList
ArrayList of primitives?
// illegal -- int cannot be a type parameter ArrayList
// creates a list of ints ArrayList