当前位置:网站首页>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);
}
}
}
边栏推荐
- Inno setup production and installation package
- Advanced API (multithreading 02)
- Advanced APL (realize group chat room)
- MySQL transaction rollback, error points record
- Specified interval inversion in the linked list
- Advanced API (use of file class)
- 高并发内存池
- 《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
- centos php7.2.24升级到php7.3
- 10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
猜你喜欢

4279. 笛卡尔树

Pits encountered in the use of El checkbox group

Inno setup production and installation package

4279. Cartesian tree

Specified interval inversion in the linked list

Basic components and intermediate components

File operation serialization recursive copy

带你全流程,全方位的了解属于测试的软件事故

dataworks自定义函数开发环境搭建

C code production YUV420 planar format file
随机推荐
【无标题】
7.2刷题两个
Margin left: -100% understanding in the Grail layout
Advanced API (local simulation download file)
PHP install the spool extension
3311. Longest arithmetic
Flask Foundation
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
C代码生产YUV420 planar格式文件
dataworks自定义函数开发环境搭建
C WinForm framework
POI excel percentage
【最详细】最新最全Redis面试大全(50道)
Thoughts on project development
TCP cumulative acknowledgement and window value update
"Moss ma not found" solution
GStreamer ffmpeg avdec decoded data flow analysis
php artisan
Distributed ID
docker建立mysql:5.7版本指定路径挂载不上。