Computer Science 15-111 (Sections A & B), Spring 2007

Class Notes:  24-Jan-2007

 

Quiz:  quiz1 on Chapters 1-3 and App A.1, B.1, and B.2, on Thu 25-Jan.

 

Topic Outline:

1.      How to Study (A Handy Guide for Freshmen in their First Advanced College Course)

a.      Attend lecture and recitation!

b.      Do the reading!  All of it!  Carefully!  Critically!

c.      Do some unassigned problems (say, from the reading)!  Really!

d.      Study the lecture notes!  Carefully!  Critically!  Run the examples!

e.      Work with classmates, friends, course staff, others!!!

f.        Start homework early, ask questions often!

g.      Think, think, think!!!

2.      “Addition by subtraction” and Two’s Complement
From earlier notes: “So subtraction is addition (less silicon!), and no +-0”

3.      Read:  Dr. Adamchik’s Notes on Arrays

4.      Strings and immutability

a.      Not mutable
   public static void failedStringChange(String s) {
     s = "no effect"; // does not change caller's string!
   }

b.      So String “+” is very expensive!
   String s = "this " + " makes " + " garbage!";

5.      StringBuffer and StringBuilder:  Mutable strings!

a.      StringBuilder is faster but not thread-safe

b.      Example:
   StringBuilder sb = new StringBuilder();
   for (char c='A'; c<'E'; c++)
     sb.append(c);  // appends with no garbage!
   String s = sb.toString();
   System.out.println(s); // prints ABCD


6.      Copying Arrays

a.      Looping structure
   int[] a = { 1, 3, 5 };
   int[] b = new int[a.length];
   for (int i=0; i<a.length; i++) b[i] = a[i];
   System.out.println(a == b); // false
   System.out.println(Arrays.equals(a,b)); // true

b.      System.arraycopy()
Replace:
   for (int i=0; i<a.length; i++) b[i] = a[i];
with:
      System.arraycopy(a,0,b,0,a.length);

c.      clone()
Replace:
   int[] b = new int[a.length];
   for (int i=0; i<a.length; i++) b[i] = a[i];
with:
      int[] b = (int[]) a.clone();

7.      Copies are shallow (they share the same objects)
   Object[] a = { new StringBuilder("ab"), "cd" };
   Object[] b = (Object[]) a.clone();
   System.out.println(Arrays.toString(a)); // [ab, cd]
   System.out.println(Arrays.toString(b)); // [ab, cd]
   ((StringBuilder)b[0]).append('E');
   b[1] = b[1] + "F";
   System.out.println(Arrays.toString(a)); // [abE, cd]
   System.out.println(Arrays.toString(b)); // [abE, cdF]
   System.out.println(a[0] == b[0]);       // true


8.      Arrays.sort
   int[] a = { 2, 4, 1, 3 };
   Arrays.sort(a);
   System.out.println(Arrays.toString(a)); // [1, 2, 3, 4]


9.      Arrays and the for-in iterator
   int[] a = { 2, 4, 1, 3 };
   for (int i : a) System.out.println(i);


10.  Primitives, Objects, Autoboxing and Auto-unboxing
   Integer i = 5;      // illegal before Java 1.5
   int j = i + 1;      // ditto
   int[]    a = { i }; // ditto
   Object[] b = { j }; // ditto
   System.out.println(i.getClass()); // java.lang.Integer
   System.out.println(j.getClass()); // Will not compile!


11.  ArrayLists:  Variable-length arrays
   ArrayList<Object> a = new ArrayList<Object>();
   a.add("ab");
   a.add(12);  // autoboxes!
   a.add(true); // ditto
   System.out.println(a.size());     // 3

System.out.println(a.toString()); // [ ab, 12, true ]
String s = (String) a.get(0);     // must cast!
System.out.println(s);            // ab

ArrayList<String> b = new ArrayList<String>();

b.add("cd");

String t = b.get(0);              // no cast!

System.out.println(t);            // cd

12.  WordSearch example:  In recitation tomorrow