[ 07 ]
. java.lang Object, Math, String, StringBuffer Byte, Short, Integer, Long, Float, Double, Boolean, Character. java.util Random, StringTokenizer Calendar, GregorianCalendar, Date. Collection, List, Queue, Set, Map LinkedList, Vector, HashSet, HashMap 2
1. JAVA.LANG 3
Section 1 java.lang p256 API (java class library) API http://docs.oracle.com/javase/7/docs/api 4
Section 1 java.lang java. javax. org. com. 5
Section 1 java.lang java.lang java.lang Object, Integer, String, StringBuffer, Math import 6
Object Section 1 java.lang Object Object tostring() (overriding) " @16 " equals() 7
Math Section 1 java.lang 8
Object Math 7-1 Section 1 java.lang 9
8 (wrapper class) Section 1 java.lang 10
Section 1 java.lang Integer Integer Integer age = new Integer(20); Integer snum = new Integer("20121105"); int int intvalue() 11
Section 1 java.lang Jdk 1.5 Double 2.59 Double radius = new Double(2.59); Jdk 1.5 (boxing) Double radius = 2.59; //boxing double r double r = radius.doublevalue(); (unboxing) double r = radius; //unboxing 12
Character Section 1 java.lang 7-4 13
Section 1 java.lang String(1) String 14
String(2) Section 1 java.lang 15
String(3) Section 1 java.lang 7-5 16
StringBuffer(1) Section 1 java.lang (capacity) StringBuffer StringBuffer sb1 = new StringBuffer(); // 16 StringBuffer sb2 = new StringBuffer(32); // 32 StringBuffer sb3 = new StringBuffer("java"); 17
StringBuffer(2) Section 1 java.lang, StringBuffer 18
StringBuffer(3) Section 1 java.lang 7-6 19
2. JAVA.UTIL 20
java.util Section 2 java.util p268 Calendar, GregorianCalendar, Date Random (random number) Vector, Stack Collection, Set, List, Queue, Map 21
Section 2 java.util Random int, long, float, double long Random rnd1 = new Random(); Random rnd2 = new Random(45); 22
Section 2 java.util 7-7 23
StringTokenizer(1) (token) (delimiters) " \t\n\r\f" \t 4 StringTokenizer 2 Section 2 java.util StringTokenizer 3 true String str = "public static void main(string[] args) {"; StringTokenizer st1 = new StringTokenizer(str); StringTokenizer st2 = new StringTokenizer(str, " ()"); StringTokenizer st3 = new StringTokenizer(str, " ()[] {"); StringTokenizer st4 = new StringTokenizer(str, "()[]{", true); 24
StringTokenizer(2) StringTokenizer Section 2 java.util 25
Section 2 java.util 7-8 26
Section 2 java.util Date Date deprecated Date Calendar, 1/1000 millisecond gettime() 1970 1 1 00:00:00 millisecond 1970 1 1 00:00:00 epoch DateFormat java.text Locale Date now = new Date(); System.out.println(now); System.out.println(now.getTime()); java.util 27
Section 2 java.util 7-9 28
Section 2 java.util Calendar getinstance() gettime() Date, Calendar now = Calendar.getInstance(); System.out.println(now.getTime()); 29
Calendar Section 2 java.util 7-10 import java.util.calendar; public class CalendarTest { public static void main(string[] args) { Calendar now = Calendar.getInstance(); System.out.println(now.getTime()); " "); } } int year = now.get(calendar.year); int month = now.get(calendar.month) + 1; // 0 int date = now.get(calendar.date); int hour = now.get(calendar.hour_of_day); int minute = now.get(calendar.minute); int second = now.get(calendar.second); System.out.println(year + " " + month + " " + date + " "); System.out.println(hour + " " + minute + " " + second + 30
3. 31
Section 3 p276 collection framework Collection, Set, List, Queue, Map 32
Section 3 Collection Collection Map Collection Set List Queue Set List Queue Map (key) (value) 33
Section 3 HashMap [ ][ ] HashMap Hash Map 34
Section 3 Collection Collection, List, Set, Queue 35
Iterator Section 3 iterator() Iterator Collection c iterator() Iterator hasnext() next() Iterator it = c.iterator(); while ( it.hasnext() ) { } System.out.println(it.next()); 36
Section 3 List List LinkedList, ArrayList, Vector, Stack 37
ListIterator Section 3 Iterator 38
LinkedList List Queue null addfirst() : add() addlsat() : add(index) Section 3 39
LinkedList Section 3 40
Section 3 7-12 41
Section 3 Vector(1) capacity 2 Vector<Object> Object Vector<Object> data = new Vector<Object>(); data.addelement(2012); data.add(" "); 42
Vector(2) Section 3 43
Section 3 7-13 import java.util.vector; public class VectorTest { public static void main(string[] args) { Vector<Object> data = new Vector<Object>(3); //Vector data = new Vector(3); // data.addelement(2012); data.add(" "); data.addelement(4.35); data.add(2, " "); data.insertelementat(" ", 0); System.out.println("size = " + data.size()); System.out.println("capacity = " + data.capacity()); System.out.println(data.toString()); } } 44
Section 3 Set Collection Set TreeSet Set SortedSet Set HashSet (hash table) Set Collection Collection 45
Section 3 7-14: HashSet Set 7 HashSet Integer HashSet<Integer> public class void HashSetTest { HashSet<Integer> seta = new HashSet<Integer>(); HashSet<Integer> setb = new HashSet<Integer>(); seta.add(3); seta.add(5); seta.add(7); seta.add(8); seta.add(7); seta.add(9); System.out.print("A = "); print(seta); System.out.println(" A = " + seta); setb.add(5); setb.add(3); setb.add(2); System.out.print("B = "); print(setb); System.out.println(" B = " + setb); boolean ischanged = seta.removeall(setb); System.out.print("A - B = "); if (ischanged) System.out.println(setA);; } } public static void print(hashset<integer> s) { Iterator<Integer> i = s.iterator(); while ( i.hasnext() ) System.out.print(i.next()+ " "); } 46
Section 3 Map (1) (key) (value) put(key, value) get(key) HashMap, TreeMap HashMap Map 47
Map (2) Section 3 48
HashMap Section 3 Map null 7-15 import java.util.hashmap; import java.util.iterator; public class HashMapTest { public static void main(string[] args) { HashMap<String, String> hm = new HashMap<String, String>(); hm.put(" ", " "); hm.put(" ", " "); hm.put(" ", " "); hm.put(" ", " "); hm.remove(" "); hm.put(" ", " "); System.out.print(" : " + hm.keyset()); System.out.println(" : " + hm.values()); } Iterator<String> keys = hm.keyset().iterator(); while ( keys.hasnext() ) { String key = keys.next(); String value = hm.get(key); System.out.println(key + ": " + value); } 49
4. 50
Section 4 p288 Generic (generic type) HashMap<String, String> Map String String <Object, Object> 51
Section 4 Generic MyContainer<E> E MyContainer (element) MyContainer<E> MyContainer<String> pl pl.add( algol ) MyContainer<Object> 52
Section 4 7-16 package generics; import java.util.arraylist; public class MyContainer<E> { private ArrayList<E> list; public MyContainer() { list = new ArrayList<E>(); } public E get(int index) { return list.get(index); } public void add(e element) { list.add(element); } 07 } public static void main(string[] args) { MyContainer<String> pl = new MyContainer<String>(); pl.add("algol"); pl.add("c"); //pl.add(5); pl.add("java"); System.out.println(pl.get(0) + " "); System.out.println(pl.get(1) + " "); System.out.println(pl.get(2) + " "); } 53
Generic Section 4 get() <T> static <T> T <T> ary T[] 7-17 package generics; public class MyGenerics { public static <T> T get(t[] ary, int index) { return ary[index]; } public static <T> T getlast(t[] ary) { return ary[ary.length-1]; } public static void main(string[] args) { Integer n[] = {3, 4, 5, 7}; System.out.println(MyGenerics.get(n, 2) + " " + MyGenerics.getLast(n)); String s[] = {"generics", "type casting", "input", "output"}; System.out.println(MyGenerics.get(s, 1) + " " + MyGenerics.getLast(s)); } } 07 54
Section 4 (enumeration data type), enum pl c, cpp, java, csharp values() switch case public class EnumTest { public enum pl {c, cpp, java, csharp}; public static void main(string[] args) { pl clang = pl.c; // System.out.println(clang); // clang = pl.csharp; switch(clang) { case csharp: System.out.println(clang + ": C# "); } } } for (pl p : pl.values()) System.out.print(p + " "); System.out.println(); 55
56
[ Add your company slogan ] Thank You!