CS51 - Spring 2010 - Lecture 20

  • Book problem 15.4.1
       - university enrolls int NUM_STUDENTS students
       - students can take one or more courses
       - Grades for each course are between 0.0 and 4.0
       - Calculate the average for each student:
       public double[] getAverage(double[][] grades)   

       - Note: it's convenient to think about 2D arrays as matrices, but they're not always matrices. Sometimes, you will have rows without the same number of columns. A 2D array is an array of arrays!

  • nibbler lab
       - due Tuesday night (24 hour extension)

  • Strings
       - Where have we seen them so far?
          - "This is a string"
          - ""+10 // gives us the String version of 10
          - Label label;
             - label.getText();
          - String[] words;
          - System.out.println(String);
       - What methods might we want?
          - concatenation: +
          - substring
          - indexOf
          - startsWith
          - endsWith
          - trim
          - split
          - equals
          - compareTo
          - length
          - replace
          - lowercase
          - uppercase
       - Strings are a class and are therefore objects
       - They're also a built in type
       - Strings are immutable!
          - you cannot change a String
          - all the String methods return a new String
       - How do you think they're implemented?
          - Array of characters... more on characters later

  • Some examples
       - String test = " A string";
       - test.substring(0, 2);
          " A"
       - test.substring(2, 7);
          " a stri"
       - test.substring(7);
          "ng"
       - test.startsWith(" A ");
          true
       - test.startsWith(" a");
          false
       - test.endsWith("n");
          false
       - test.endsWith("string");
          true
       - test.toLowercase();
          " a string";
       - test.toUppercase();
          " A STRING";
       - test.trim();
          "A string"
       - test.indexOf("i");
          6
       - test.indexOf("ring");
          5
       - test.indexOf("A string");
          1
       - test.indexOf("banana");
          -1
       - " This is a string".indexOf("i", 4);
          6
       - test.replace("s", "S");
          " A String"
       - test.replace("stri", "ris");
          " A rising"
       - "This is a string".replace(i, "");
          "Ths s a strng";
       - test.split(" ");
          ["", "A", "string"];
       - test.trim().toUppercase().substring(3);
          "TRING";
       - test.toLowercase().trim().substring(5, 7).startsWith("i");
          true
       - test.subString(0,3) + "longer " + test.subString(3);
          " A longer string"
       - test.length();
          9
       - test.toCharArray()
          [' ', 'A', ' ', 's', 't', 'r', 'i', 'n', 'g']
       - test == " A string";
          false
       - test.equals(" A string");
          true
       - test.equals(" a string");
          false
       - test.equalsIgnoreCase(" a string");
          true
       - " A string" == " A string"
          true
          (Java uses a pool of Strings for string literals, so if a String literal occurs multiple times, it actually refers to the same String. Bottom line, just use .equals!)

  • show StringDemos demo

  • http://www.sr.se/P1/src/sing/#