Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Chapter 6 Java Generic Type 1 [email protected] Xiang Zhang Content 2 Significance of Generic Type Definition of Generic Type Usage of Generic Type Java CoSE Significance of Generic Type 3 What is Generic Type Why Generic Type? How about the inconvenience without GT? Java CoSE Significance of Generic Type 4 Example Write a MyArrayList Without GT The element is Object Think: Is there any problem? How to solve? If we add a Person to MyArrayList, what can we get() later? Java CoSE Type loss Significance of Generic Type 5 Example Write MyArrayList using GT GT improves the simplicity and correctness Java CoSE Significance of Generic Type 6 Merit of GT Type-safe Eliminate Type Casting Usually used in defining type of elements in Collections Java CoSE Definition of Generic Type 7 Generic Class Generic Interface Java CoSE Definition of Generic Type 8 Bounded Type Parameter (受限类型参数) Sometimes, parameter type in GT should be limited Can Person be the parameter type in SortedCollection? Java CoSE Definition of Generic Type 9 Bounded Type Parameter The meaning Keyword extends is generic Comparable interface is called upper bound E is called a Bounded Type Parameter Java CoSE 泛型的定义 10 Bounded Type Parameter Multiple-bounded Java CoSE Usage of Generic Type 11 Example: Write a Static Method to Sum a List 1. without GT Short: We do not know the element type, and cannot guarantee each element in List L is able to sum up. Java CoSE Usage of Generic Type 12 2. With GT Java CoSE What We Got 13 13 The runtime type of an object is determined by: Its class Its class parameter So Passing ArrayList<Number> to List<Number>, OK! Passing List<Integer> to List<Number>, NO!! List<Integer> is not a subclass of List<Number> What should we do to maintain versatality? Java CoSE Usage of GT 14 3. With GT and Wildcard Java CoSE Some Tips 15 ArrayList<Number> a = new ArrayList<Integer>(); // right or not? ArrayList<? extends Number> a = new ArrayList<Integer>(); // right or not? In invocation of method public void add(Number a), what type of objects can we transfer into a method as parameter Just the type Number // right or not? Number and all its subtypes // right or not? Java CoSE Usage of Generic Type 16 Usage of Wildcard Define Upper bound Define Lower bound List<? super Integer> // Pls give 5 matching types Multiple-bounded is not allowed List<? extends Number> List<? extends Number & Serializable> // Compiling error Guess: is List<?> different with List<Object>? Java CoSE 17 Example 18 Example 19 Example Self-study 20 Text Processing Basic elements in text processing (chapter 7) String / StringBuffer / StringBuilder // try to benchmark Scanner StringTokenizer Java and IR (Information Retrieval) Tokenizing (分词) Stopwords Removal (去除停用词) Rooting (取词根) Indexing (索引) Java CoSE Forecast 21 AWT and Swing Introduction Swing Container(JFrame, JPanel) Swing Components Layout Manager Event and Event-based Programming Menu Java CoSE