Using a generic instructor: Wang Shaohua QQ Group: 483773664
Learning Goals:
Understanding the need to use generics
Mastering the use of Java generics
First, why use generics
The Java collection has one drawback: the Java collection defaults to the object type for the elements of our "Throw in" collection, without remembering the type of the element itself. Of course, Java is designed for a reason: Because programmers who design collections do not know what types of objects we need to store with this collection, they design collections to save any type of ease of use.
This design has the following problems
The collection has no restrictions on the element type, which can cause some problems. For example, we create a collection to hold the person object, but the program can easily put the dog object into the collection.
Because the collection does not remember the type of the element itself, the cast is coerced when the elements are taken out. This can happen "type conversion exception (ClassCastException)"
1234567891011121314151617 |
public class ListErr {
public static void main(String[] args) {
//创建一个List,只想用这个List来保存字符串
List strList =
new ArrayList();
strList.add(
"孙悟空"
);
strList.add(
"猪八戒"
);
//不小心,将Integer类型的数据添加进去了
strList.add(
500
);
//遍历取值(strList里取出的值都是Object类型)
for (Object object : strList) {
//可进行类型转换
String name = (String) object;
//进行到最后一个元素的时候,会发生类型转换错误
System.out.println(name);
}
}
}
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/80/3A/wKioL1c78gHwkvUOAAAbQiRxpEY619.png " data_ue_src= "E:\My knowledge\temp\d9ce9596-868c-4544-b453-fdbe5d4835e2.png" >
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/80/3D/wKiom1c78ReyZaHnAAAqJMOS4hI245.png " data_ue_src= "E:\My knowledge\temp\b2f29e62-f374-4716-a097-9a9b2d0cbbee.png" >
Summary: To use a generic (Generic) cause
Addressing security issues with element storage
A type cast issue is required when resolving the Get data element
Ii. Manual implementation of type checking (i), code implementation and validation
If we want a list object that can only hold a string object, then we need to extend the ArrayList. The code is as follows
1234567891011121314151617181920212223 |
public class StrList {
private List strList =
new ArrayList();
/**
* 重写添加元素方法
* @param str
* @return
*/
public boolean add(String str){
return strList.add(str);
}
/**
* 重写get方法
* @param index
* @return
*/
public String get(
int index) {
return (String) strList.get(index);
}
public int size(){
return strList.size();
}
}
|
Use the list above
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/80/3A/wKioL1c78gGiYqL3AAApPAPGTyk067.png " data_ue_src= "E:\My knowledge\temp\c2e72aea-9937-44be-b4b8-c9a6de2cf9c9.png" >
1234567891011121314 |
public class TestStrList {
public static void main(String[] args) {
//创建一个只想保存字符串的List集合
StrList strList =
new StrList();
strList.add(
"孙悟空"
);
strList.add(
"猪八戒"
);
//将其他类型添加进去,就是编译不通过
//strList.add(500);
for (
int i=
0
;i<strList.size();i++) {
//无须强制类型转换
String name = strList.get(i);
}
}
}
|
(b), the question comes
Although the above method has solved the two drawbacks of the Java collection, it is a horrible thing to apply this implementation to the actual project development. Because we're going to define a lot of list subclasses. Java engineers are certainly not so silly, so how does Java solve these problems?
Iii. using generics (i) Use generics
From JDK1.5 onwards, Java allows us to specify the type of the collection element when creating the collection. The parameterized type of Java is called a generic
1234567891011121314 |
public class GenericList {
public static void main(String[] args) {
//创建一个只想保存字符串的List集合
List<String> strList =
new ArrayList<String>();
strList.add(
"孙悟空"
);
strList.add(
"猪八戒"
);
//将其他类型添加进去,就是编译不通过
//strList.add(500);
//无须强制类型换
for (String string : strList) {
System.out.println(string);
}
}
}
|
(ii) No generics are specified when the object is instantiated, the default is Object
12345678 |
public class GenericTest { public static void main(String[] args) { List list = new ArrayList(); for (Object object : list) { } } } |
(iii) different references to generic types cannot be assigned to each other
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/80/3D/wKiom1c78Rji6EdZAAAfk50po-E586.png " data_ue_src= "E:\My knowledge\temp\1a849118-32ac-4f6d-856a-26d622eeb39f.png" >
(iv) When using generics, the generic type must be a reference type and cannot be a basic data type
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/80/3A/wKioL1c78gKywq7EAAAVfvZ3RYg511.png " data_ue_src= "E:\My knowledge\temp\230ba673-a6c9-4e56-a5f2-3e2a4f118dd6.png" >
Iv. Learning Video URL:
Http://edu.51cto.com/course/course_id-6083.html
From for notes (Wiz)
Learn from teacher Wang Generics (i) using generics