当前位置:网站首页>Use of generics
Use of generics
2022-07-03 07:21:00 【four thousand three hundred and forty-three】


package Generic;
import org.junit.Test;
import java.util.*;
public class GenericTest {
@Test
public void test1(){
// Cases where generics are not used :
ArrayList list = new ArrayList();
// demand : Store student scores
list.add(89);
list.add(87);
list.add(86);
list.add(90);
// problem 1: The type is not safe
// list.add("tom");
for (Object obj : list){
// problem 2: Type conversion exceptions may occur during strong conversion
int stuScore = (int)obj;
System.out.println(stuScore);
}
}
// Using generics in collections
// Generic is a class , Cannot be a basic data type
@Test
public void test2(){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(78);
list.add(79);
list.add(80);
list.add(77);
// Type checking occurs at compile time , Ensure data security
// list.add("Tom");
int stuScore;
for(Integer score: list){
// The forced rotation operation is avoided
stuScore = score;
System.out.println(stuScore);
}
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
stuScore = iterator.next();
System.out.println(stuScore);
}
}
// Using generics in collections :HashMap
@Test
public void test3(){
Map<String ,Integer> map = new HashMap<String, Integer>();
map.put("Laowang",40);
map.put("Xiaodu",10);
map.put("Xiaoli",3);
map.put("Laohei",95);
// The nesting of generics appears
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
边栏推荐
- In depth analysis of reentrantlock fair lock and unfair lock source code implementation
- Arduino Serial系列函数 有关print read 的总结
- Laravel Web框架
- IP home online query platform
- Advanced API (local simulation download file)
- Thoughts on project development
- Selenium key knowledge explanation
- PHP install composer
- 2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
- 3311. Longest arithmetic
猜你喜欢

Deep learning parameter initialization (I) Xavier initialization with code

High concurrency memory pool

高并发内存池

4279. 笛卡尔树

Final, override, polymorphism, abstraction, interface

Spa single page application

Liang Ning: 30 lectures on brain map notes for growth thinking

C代码生产YUV420 planar格式文件

Pits encountered in the use of El checkbox group
![[solved] unknown error 1146](/img/f1/b8dd3ca8359ac9eb19e1911bd3790a.png)
[solved] unknown error 1146
随机推荐
Summary of abnormal mechanism of interview
Wireshark software usage
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
Mise en place d'un environnement de développement de fonctions personnalisées
【已解决】Unknown error 1146
Longest common prefix and
[HCAI] learning summary OSI model
[plus de détails] dernière entrevue complète redis (50)
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
TCP cumulative acknowledgement and window value update
Setting up the development environment of dataworks custom function
Resthighlevelclient gets the mapping of an index
Interfaces and related concepts
Upgrade CentOS php7.2.24 to php7.3
Advanced API (batch image Download & socket dialog)
[cmake] cmake link SQLite Library
C code production YUV420 planar format file
Discussion on some problems of array
Advanced API (multithreading)