200 likes | 457 Views
Chapter 6 Java Generic Type. x.zhang@seu.edu.cn. Content. Significance of Generic Type Definition of Generic Type Usage of Generic Type. Significance of Generic Type. What is Generic Type Why Generic Type? How about the inconvenience without GT?. Significance of Generic Type. Example
E N D
Chapter 6 Java Generic Type x.zhang@seu.edu.cn
Content • Significance of Generic Type • Definition of Generic Type • Usage of Generic Type Java CoSE
Significance of Generic Type • What is Generic Type • Why Generic Type? • How about the inconvenience without GT? Java CoSE
Significance of Generic Type • Example • Write a MyArrayList • Without GT • The element is Object • Think: Is there any problem? How to solve? Java CoSE
Significance of Generic Type • Example • Write MyArrayListusing GT • GT improves the simplicity and correctness Java CoSE
Significance of Generic Type • Merit of GT • Type-safe • Eliminate Type Casting • Usually used in defining type of elements in Collections Java CoSE
Definition of Generic Type • Generic Class • Generic Interface Java CoSE
泛型的定义 • 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 • Bounded Type Parameter • The meaning Keyword extends is generic • Comparableinterface is called upper bound • Eis called a Bounded Type Parameter Java CoSE
泛型的定义 • Bounded Type Parameter • Multiple-bounded Java CoSE
Usage of Generic Type • 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 • 2. With GT Java CoSE
Usage of GT • 3. With GT and Wildcard Java CoSE
Some Tips • 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 • Usage of Wildcard • Define Upper bound • List<? extends Number> • Define Lower bound • List<? super Integer> • List<Integer>, List<Number>, List<Serializable>, List<Comparable<Integer>>, List<Object> • Multiple-bounded is not allowed • List<? extends Number & Serializable> // Compiling error • Guess: is List<?> different with List<Object>? Java CoSE
Self-study • Text Processing • Basic elements in text processing (chapter 7) • String / StringBuffer / StringBuilder // try to benchmark • Scanner • StringTokenizer • Javaand IR (Information Retrieval) • Tokenizing (分词) • Stopwords Removal (去除停用词) • Rooting(取词根) • Indexing (索引) Java CoSE
Forecast • AWTand SwingIntroduction • SwingContainer(JFrame, JPanel) • Swing Components • Layout Manager • Eventand Event-based Programming • Menu Java CoSE