Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA Lists and Files. Objectives Be able to use ArrayLists: Create Initialize Add elements Retrieve elements Pass as arguments to methods Return from.

Similar presentations


Presentation on theme: "JAVA Lists and Files. Objectives Be able to use ArrayLists: Create Initialize Add elements Retrieve elements Pass as arguments to methods Return from."— Presentation transcript:

1 JAVA Lists and Files

2 Objectives Be able to use ArrayLists: Create Initialize Add elements Retrieve elements Pass as arguments to methods Return from methods Be able to use Random to get a random number Be able to parse input from a file Be able to write output to a file

3 3 Example: Analysis and Design We’d like to make a guessing game that drills students on the countries of the world. Elements: A GUI controller; A Game class that: Represents a list of countries; Randomly chooses a country as the current answer; Produces a sequence of hints. A Country class that: Represents stuff about countries. Your guess… Give up Some hint goes here (maybe an image and/or text)…

4 4 Limitations of Arrays Our initial iteration assumes that: We know how many countries there are; We’re happy with the low-level array methods. Moving beyond these assumptions forces us to give up arrays because arrays: Are fixed in size at compile time; Have a limited set of predefined methods.

5 5 Lists Java’s List data structure is more flexible: Lists can grow or shrink at run time. Lists provide more predefined behaviors. The Java Collections framework provides classes supporting groups of objects: List <> specifies an interface for an ordered collection of typed objects; ArrayList<> implements the List<> interface using an array.

6 6 The ArrayList Class ArrayLists store an array of typed objects. List aList = new ArrayList (); aList size array 0

7 7 ArrayLists: Adding Values (add) ArrayLists handle their own memory allocation. List aList = new ArrayList (); aList.add(aTypeObject); aList [0] [1] … [m-1] size array 1 aTypeObject

8 8 ArrayLists: Accessing Values (get) ArrayLists provide indexed access. List aList = new ArrayList (); aList.add(aTypeObject); System.out.println(aList.get(arrayIndex)); aList size array 1 [0] [1] … [m-1] aTypeObject

9 9 ArrayLists: Memory Allocation ArrayLists allocate memory automatically. List aList = new ArrayList (); aList.add(aTypeObject); System.out.println(aList.get(arrayIndex)); aList.add(a2ndTypeObject);... aList.add(anM+1stTypeObject); aList size array m+1 aTypeObject [0] [1] … [m-1][m] … a2ndTypeObjectanM+1stTypeObject

10 Random To get a random number in Java, create a random number generator: Random randomGenerator = new Random(); The generator will return a new random number each time it is asked to do so: int randomNumber1 = randomGenerator.nextInt(); int randomNumber2 = randomGenerator.nextInt(15); double randNumber3 = randomGenerator.nextDouble();

11 Using lists for our example We’d like to have an arbitrary number of countries in the “list”. We’ll keep the same GUI controller, but will create an arbitrarily long List of country name Strings. Upgrade the CountryGuessGame to use lists rather than arrays. The results of this programming should be as follows: CountryGuessController1 should not change; CountryGuessGame should end up looking like CountryGuessGame1 (handout).

12 package c12lists.lecture.countryguess; import java.util.ArrayList; import java.util.List; import java.util.Random; public class CountryGuessGame { private List myCountries; private int myAnswerIndex, myHintCount; private Random myRandom; public CountryGuessGame() { myCountries = new ArrayList (); myCountries.add("Honduras"); myCountries.add("Panama"); myRandom = new Random(); reset(); } public void reset() { myAnswerIndex = myRandom.nextInt(myCountries.size()); myHintCount = 0; } //continued on next slide

13 //continued from previous slide public boolean guess(String text) { return myCountries.get(myAnswerIndex).equalsIgnoreCase(text); } public String getAnswer() { return myCountries.get(myAnswerIndex); } public String getHintText() { myHintCount++; String name = myCountries.get(myAnswerIndex); if (myHintCount == 1) { return "The name starts with '" + name.charAt(0) + "'"; } else if (myHintCount == 2) { return "The name has " + name.length() + " letters"; } else if (myHintCount == 3) { return "The name ends with '" + name.charAt(name.length() - 1) + "'"; } else { return "no more hints"; } } //ends CountryGuessGame class

14 14 Array & Lists Syntax String[] myCountries = new String[2]; myCountries[0] = "Honduras"; myCountries[1] = "Panama"; System.out.println(myCountriesArray.length); System.out.println(myCountriesArray[0]); List myCountriesList = new ArrayList (); myCountriesList.add("Honduras"); myCountriesList.add("Panama"); System.out.println(myCountriesList.size()); System.out.println(myCountriesList.get(0));

15 15 Modeling Countries

16 16 ArrayLists can be passed as parameters. ArrayLists: As Parameters private int count(List countries, String continent) { int result = 0; for (int i = 0; i < countries.size(); i++) { if (countries.get(i).getContinentName().equalsIgnoreCase(continent)) { result++; } return result; }

17 17 ArrayLists can be returned as return values. ArrayLists: As Return values private List loadCountries() { List result = new ArrayList (); result.add(new Country("Algeria", "Africa")); result.add(new Country("Angola", "Africa"));... return result; }

18 18 ArrayList: Copying List original = new ArrayList (); // add two country objects to original (c1 & c2)… List referenceCopy = original; List shallowCopy = (List )original.clone(); original size array 2 c1c2 referenceCopy shallowCopy size array 2

19 19 List deepCopy = deepCopy(original); public List deepCopy(List original) { List result = new ArrayList (); for (int i = 0; i < original.size(); i++) result.add(new Country(myCountries.get(i).getName(), myCountries.get(i).getContinentName(), myCountries.get(i).getImageName())); return result; } deepCopy size array 2 c1copyc2Copy

20 20 ArrayList Equality Similar issues arise when checking arraylist equality: anArrayList.equals(anotherArrayList) checks the two lists are the same size and that their corresponding elements are equals(). This works for lists of strings, but special equality checking routines must be written for lists of other types. The String class has an equals() operator that checks string equality properly.

21 21 Multi-Dimensional Lists Lists can also be multi-dimensional. Declaring 2-D lists: ArrayList > ID Initializing 2-D lists: new ArrayList >(rowsize) Accessing 2-D array elements: ID.get(row).get(column) Multidimensional arrays are generally easier to use and more efficient.

22 Multi-dimensional List Structures Multi-dimensional lists are useful for more general multi- dimensional structures. Example: 22

23 23 Example: Character Drill We’d like to modify the guessing game so that drills students on Chinese Characters. A sketch of a solution achieving this goal is shown here. User’s guess… Give up Some hint goes here (maybe an image and/or audio)…

24 24 Example: Design The design includes the following classes:

25 Limitations of Hard-Coding Data Our initial iteration assumes that: we can code the data directly in the program; the data never (or rarely) changes; people who change data know how to program. This approach does not scale well to real data-based applications. 25

26 26 Input & Output Streams Input and output in Java is accomplished using stream classes: Program Input Stream Output Stream Program

27 27 Java Streams Simple I/O uses predefined streams: System.in System.out System.err Create file streams using: File Scanner PrintWriter

28 28 File The File class models a system-independent view of a file comprising: Filename; Directory pathname: Relative; Absolute. Patterns: new File(pathAndFilenameString) new File(pathnameString, filenameString)

29 29 Scanner The Scanner class can scan: Keyboard input stream; File; String. Pattern: new Scanner(inputStreamOrFileOrString) The API includes these methods: next() nextInt() nextLine()... hasNext() hasNextInt() hasNextLine()... close()

30 Example: Compute Statistics Problem: Compute statistics for a file of quiz scores. Given: the data filename and path Algorithm: Open a read stream/scanner to the given file. While the file has more tokens: Read the token. Process the token. Close the file stream/scanner.

31 import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Statistics { public static final String PATH = “src/c11files/examples/data/”; //main method will go here... //compute methods will go here... } Statistics Solution Structure Method implementations on next slides

32 public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Data filename: "); String filename = keyboard.next(); try{ Scanner fileIn = new Scanner(new File(PATH, filename)); List scores = new ArrayList (); while (fileIn.hasNext()) { scores.add(fileIn.nextInt()); } fileIn.close(); System.out.println(scores); double average = computeAverage(scores); System.out.printf("average: %5.2f%n", average); System.out.printf("std dev: %5.2f%n", computeVariance(scores, average)); } catch(FileNotFoundException e){ System.out.println("Invalid file indicated."); }

33 public static double computeVariance( List values, double average) throws Exception { if (values == null || values.size() == 0) { throw new Exception("Empty list"); } int sum = 0; for (int i = 0; i < values.size(); i++) { sum += Math.pow(values.get(i) - average, 2); } return Math.sqrt(sum / values.size()); }

34 34 public static double computeAverage(List values) throws Exception { if (values == null || values.size() == 0) { throw new Exception("Empty list"); } int sum = 0; for (int i = 0; i < values.size(); i++){ sum += values.get(i); } return sum / values.size(); }

35 Example: Record Input 35 Problem: Read in soldier data from a file. Soldiers may or may not have nickname(s). Given: the data filename and path Algorithm: Open a read stream/scanner to the given file. While the file has more lines: Read the line. Process the fixed and variant tokens. Close the file stream/scanner.

36 public class ReadRecordsConsole { public static final String PATH = "src/c11files/examples/data/"; public static void main(String[] args) { try{ Scanner keyboard = new Scanner(System.in); System.out.print("Data Records filename: "); String filename = keyboard.next(); Scanner fileIn = new Scanner(new File(PATH, filename)); List soldiers = new ArrayList (); while (fileIn.hasNextLine()) { soldiers.add(new Soldier(fileIn.nextLine())); } fileIn.close(); for (int i = 0; i < soldiers.size(); i++) { System.out.println(soldiers.get(i)); } catch(FileNotFoundException e){ System.out.println("Invalid File indicated."); }

37 public class Soldier { private String myName, myRank, mySerialNumber; private List myNickNames; public Soldier(String line) { Scanner scanner = new Scanner(line); myName = scanner.next(); myRank = scanner.next(); mySerialNumber = scanner.next(); myNickNames = new ArrayList (); while (scanner.hasNext()) { myNickNames.add(scanner.next()); } scanner.close(); } public String toString() { String result =myName + " " + myRank + " " + mySerialNumber; for (int i = 0; i < myNickNames.size(); i++) { result += " " + myNickNames.get(i); } return result; }

38 38 PrintWriter The PrintWriter class can print formatted text to a text-output stream. Pattern: new PrintWriter(outputFile) The API includes these methods: print() println()... printf() close() flush()

39 Example: File Output 39 Problem: Get data from the user, put it into a file. Given: the output filename and path Algorithm: Open a print writer stream to the given file. Loop forever: Prompt for and read a line of data. If the line is the sentinel Quit. else Output the line. Close the file stream/scanner.

40 Example Implementation 40 PrintWriter fileOut = new PrintWriter(new File(path, filename)); String line = ""; while (true) { System.out.print("enter record (just enter to quit): "); line = keyboard.nextLine(); if (line.equals("")) { break; } else { fileOut.println(line); } fileOut.close(); System.out.println("data stored to: " + path + filename);

41 41 // header information here... public class CharacterFileBuilderController extends JFrame implements ActionListener { private JLabel messageLabel; private JTextField englishField, pinyinField, imageFilenameField, soundFilenameField, sentenceField; private JButton addButton, closeButton; private PrintWriter outFile; public CharacterFileBuilderController() throws URISyntaxException, FileNotFoundException { super("Chinese Character Creator"); setDefaultCloseOperation(EXIT_ON_CLOSE); Font font = new Font("Ariel", 0, 18); JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Choose an output filename"); fileChooser.showOpenDialog(this); outFile = new PrintWriter(fileChooser.getSelectedFile()); // GUI specification here... } @Override public void actionPerformed(ActionEvent event) { String actionCommand = event.getActionCommand(); if (actionCommand.equalsIgnoreCase("Add")) { outFile.print(englishField.getText() + " " + pinyinField.getText() + " " + imageFilenameField.getText() + " " + soundFilenameField.getText() + " " + sentenceField.getText()); messageLabel.setText("Record added..."); } else if (actionCommand.equalsIgnoreCase("Close")) { outFile.close(); messageLabel.setText("File saved..."); } // main() method here... }}

42 Character Drill Revisited

43 43 Buffering I/O is slow relative to processing speed, so it is generally buffered. Program Input Stream Output Stream Program buffer

44 44 Java Buffering Buffering can be added to file streams to improve the efficiency of I/O operations. A buffer is a memory area that stores up input/output data so that it can be input/output all at once. Patterns: new BufferedReader(new FileReader(fileNamePath)); new BufferedWriter(new FileWriter(fileNamePath));


Download ppt "JAVA Lists and Files. Objectives Be able to use ArrayLists: Create Initialize Add elements Retrieve elements Pass as arguments to methods Return from."

Similar presentations


Ads by Google