当前位置:网站首页>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);
}
}
}
边栏推荐
- [most detailed] latest and complete redis interview book (50)
- I. D3.js hello world
- “百度杯”CTF比赛 2017 二月场,Web:爆破-1
- Advanced APL (realize group chat room)
- IP home online query platform
- 2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
- The difference between typescript let and VaR
- 万卷书 - 价值投资者指南 [The Education of a Value Investor]
- Recursion, Fibonacci sequence
- 【CMake】CMake链接SQLite库
猜你喜欢

Recursion, Fibonacci sequence

Store WordPress media content on 4everland to complete decentralized storage
![Gridome + strapi + vercel + PM2 deployment case of [static site (3)]](/img/65/8d79998e96a2c74ba6e237bee652c6.jpg)
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]

SecureCRT password to cancel session recording

Discussion on some problems of array

691. Cube IV

JMeter JSON extractor extracts two parameters at the same time

Dora (discover offer request recognition) process of obtaining IP address
![[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet](/img/9d/42dfef67246740f0dba0c6d8f1b625.jpg)
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet

JUC forkjoinpool branch merge framework - work theft
随机推荐
CentOS switches and installs mysql5.7 and mysql8.0
Recursion, Fibonacci sequence
docker建立mysql:5.7版本指定路径挂载不上。
Download address collection of various versions of devaexpress
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
Centos切换安装mysql5.7和mysql8.0
docket
C代码生产YUV420 planar格式文件
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
[solved] unknown error 1146
Jeecg menu path display problem
[HCAI] learning summary OSI model
Distributed ID
专题 | 同步 异步
"Moss ma not found" solution
PAT甲级真题1166
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
Warehouse database fields_ Summary of SQL problems in kingbase8 migration of Jincang database
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
Margin left: -100% understanding in the Grail layout