CS100J, Spring 2001 Thurs 3/29 Lecture 18 ------------------------------------------------------------------------------- Announcements: + typo in P5: (a+bi) + (c+di) = (a+c) + (b+d)i + P5 extension: due Tues 4/10 + Savitch pg 402 + from 1/27: PL serial # is KYMEZ2UG + delay in L18 examples + ? on pp 152, 991 + E9 due Tues 4/3 (will be posted about 11am today) ------------------------------------------------------------------------------- Topics: + multidim array wrapup + strings/chars + searching/sorting ------------------------------------------------------------------------------- Summary from Lecture 17 + arrays of arrays + multidimensional arrays + row and column major ------------------------------------------------------------------------------- See remaining lecture 17 notes ------------------------------------------------------------------------------- Characters: + primitive type (pg 57) + stores integer codes for 65000+ characters (UNICODE) + deal mostly with ASCII (original 128 characters) (pg 992) - characters on keyboard - non-printing characters + escape, backspace, bell, new line + see \n, \t (pg 88) + syntax of type: char var = 'integer' - default value of char instance variable is NUL character (ASCII 0) - note: $''$ is not NUL character -- it's illegal + interesting tricks: 'A' to 'Z' has ASCII range 65 to 90 'a' to 'z' has ASCII range 97 to 122 --> 'a'-'A' is 32 --> 'Q'+'a'-'A' gives 'q' + arithmetic - character arithmetic give integers! - can mix ints and chars - can typecast with char to int and vice versa example) System.out.println('Q'+ 'a'-'A') gives 113 To fix and get q? --> System.out.println((char) ('Q'+ 'a'-'A')) --> char tmp = 'Q'+ 'a'-'A'; System.out.println(tmp) + see Character class in API ------------------------------------------------------------------------------- Strings: + collections of text + objects in Java + string literal: "stuff" (saves hassle of calling a constructor) + constructors? String s1 = new String(); // create empty string String s2 = new String("stuff"); // create string of "stuff" char[] tmp = {'a','b','c'}; String s3 = new String(tmp); // create string from chars + so, what is a string literal? an instance of class String -- namely, a shortcut from calling a constructor! + immutable -- once created, cannot change! see StringBuffer class for mutable strings + Resemblence of Strings to arrays: - index of characters starts count at ZERO - find number of characters with $length()$ (not $length$) + string methods... -------------------------------------------------------------------------------