当前位置:网站首页>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);
}
}
}
边栏推荐
- C WinForm framework
- JUC forkjoinpool branch merge framework - work theft
- 4EVERLAND:IPFS 上的 Web3 开发者中心,部署了超过 30,000 个 Dapp!
- Arduino 软串口通信 的几点体会
- RestHighLevelClient获取某个索引的mapping
- php artisan
- Pat grade a real problem 1166
- 4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
- Book recommendation~
- Sorting, dichotomy
猜你喜欢
4279. Cartesian tree
dataworks自定义函数开发环境搭建
Store WordPress media content on 4everland to complete decentralized storage
Final, override, polymorphism, abstraction, interface
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
Common problems in io streams
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
[set theory] partition (partition | partition example | partition and equivalence relationship)
Spa single page application
带你全流程,全方位的了解属于测试的软件事故
随机推荐
PHP install the spool extension
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
JS date comparison
【最詳細】最新最全Redis面試大全(50道)
JMeter JSON extractor extracts two parameters at the same time
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
[untitled]
树莓派更新工具链
php安装composer
Laravel frame step pit (I)
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
crontab定时任务
Download address collection of various versions of devaexpress
JMeter test result output
Jeecg request URL signature
GStreamer ffmpeg avdec decoded data flow analysis
Use the jvisualvm tool ----- tocmat to start JMX monitoring
Visit Google homepage to display this page, which cannot be displayed
Basic components and intermediate components