Java核心篇之泛型--day5

Java核心篇之泛型–day5

泛型是JDK5时引入的一个新特性,泛型提供了编译时类型安全检查的机制,该机制允许程序猿在编译时检测到非法的类型输入。
泛型的本质是参数化类型,也就是说操作的类型被指定为一个参数。

假定我们有一个需求:写一个排序方法,能够对整型数组、字符串数组甚至其他任何类型的数组进行排序,该如何实现?
答案是可以使用 Java 泛型。

泛型方法:该方法在调用的时候可以接收不同的参数类型,规则如下:

  1. 所有泛型方法声明都有一个类型参数声明部分(由尖括号分隔),该类型参数声明部分在方法返回类型之前(在下面例子中的)。
  2. 泛型方法体的声明和其他方法一样。注意类型参数只能代表引用型类型,不能是原始类型(像int,double,char的等)。
  3. 有界的类型参数:例如,一个操作数字的方法可能只希望接受Number或者Number子类的实例。这就是有界类型参数的目的。
    在这里插入图片描述

泛型类:

  1. 泛型类的声明和非泛型类的声明类似,除了在类名后面添加了类型参数声明部分。
    在这里插入图片描述

类型通配符:
2. 类型通配符一般是使用?代替具体的类型参数。例如 List<?> 在逻辑上是List,List 等所有List<具体类型实参>的父类。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558869.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring Boot————AOP入门案例及切面优先级设置

看了这篇文章&#xff0c;如果你还是不会用AOP来写程序&#xff0c;请你打我&#xff01;&#xff01; .||| 引言 Spring AOP是一个对AOP原理的一种实现方式&#xff0c;另外还有其他的AOP实现如AspectJ等。 AOP意为面向切面编程&#xff0c;是通过预编译方式和运行期动态代…

Java核心篇之HashMap--day6

Java核心篇之HashMap–day6 HashMap是一种键值对的数据结构&#xff0c;以数组与链表的形式&#xff08;key&#xff1a;value&#xff09;实现&#xff0c;查询性能和添加性能很好。他是通过将key进行hashcode&#xff08;&#xff09;映射函数来找到表中对应的位置。 HashM…

Spring Boot————Spring Data JPA简介

引言 JPA是Java 持久化API的缩写&#xff0c;是一套Java数据持久化的规范&#xff0c; Spring Data Spring Data项目的目的是为了简化构建基于Spring 框架应用的数据访问技术&#xff0c;包括对关系型数据库的访问支持。另外也包含非关系型数据库、Map-Reduce框架、云数据服…

LeetCode算法入门- Valid Parentheses -day11

LeetCode算法入门- Valid Parentheses -day11 题目描述&#xff1a; Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed …

Spring Boot————Spring Boot启动流程分析

一、引言 Spring Boot 的启动虽然仅仅是执行了一个main方法&#xff0c;但实际上&#xff0c;运行流程还是比较复杂的&#xff0c;其中包含几个非常重要的事件回调机制。在实际生产开发中&#xff0c;有时候也会利用这些启动流程中的回调机制&#xff0c;做一些项目初始化的工…

LeetCode算法入门- Longest Valid Parentheses -day12

LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. 题目描述&#xff1a; 题目的意思是给定一个括号序列的字符串&#xff…

Spring Boot————应用启动时的监听机制测试

引言 本文承接前面的《Spring Boot————Spring Boot启动流程分析》&#xff0c;主要测试一下ApplicationContextInitializer、SpringApplicationRunListener、ApplicationRunner、CommandLineRunner这四个接口实现之下的组件是何时在Spring Boot项目启动时创建并执行相关方…

LeetCode算法入门- Longest Common Prefix -day13

LeetCode算法入门- Longest Common Prefix -day13 题目描述&#xff1a; Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Example 1: Input: [“flower”,“flow”…

HTMLCSS————块元素与内联元素

一、块元素与div标签 div是一个块元素&#xff0c;块元素会独占一行&#xff0c;无论它的内容有多少&#xff0c;都会独占一整行。 类似的块元素还有&#xff1a;<p>、<h1>、<h2>、<h3> 然而&#xff0c;<div>标签没有任何语义&#xff0c;就…

LeetCode算法入门- Compare Version Numbers -day14

LeetCode算法入门- Compare Version Numbers -day14 题目描述&#xff1a; Compare two version numbers version1 and version2. If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. You may assume that the version strings a…

HTMLCSS————CSS常用选择器及优先级

选择器优先级 内联样式&#xff08;1000&#xff09;> id选择器&#xff08;100&#xff09;> 类和伪类选择器&#xff08;10&#xff09; > 元素选择器&#xff08;1&#xff09;>通配 * 选择器&#xff08;0&#xff09;> 继承的样式 一、元素选择器 作用&…

LeetCode算法入门- Merge Two Sorted Lists -day15

LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述&#xff1a; Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3-&g…

2018年度总结

2018年&#xff0c;已经成为过去式&#xff0c;这360多天依旧过的很快&#xff0c;快到当我手扶键盘回想这一年发生的点点滴滴时&#xff0c;都没有任何感慨。可能我天生是个无感之人&#xff0c;或许&#xff0c;这一年的时光&#xff0c;无数的事故、故事已经让我变得不那么感…

LeetCode算法入门- Generate Parentheses -day16

LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n 3, a solution set is: [ “((()))”, “(()())”, “(())()”, “()(())”, …

2019技术学习规划

引言 前段时间总结了一下2018年的大事小情&#xff08;《2018年度总结》&#xff09;&#xff0c;整体来说还是正能量满满&#xff0c;阅读量涨得也是蛮快的。今天&#xff0c;抽出点时间思考了一下未来一年的规划。那作为技术人才&#xff0c;规划也自然都是技术相关的&#…

LeetCode算法入门- Remove Nth Node From End of List -day17

LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释&#xff1a; Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n 2. After removing the seco…

Spring Boot————默认缓存应用及原理

引言 应用程序的数据除了可以放在配置文件中、数据库中以外&#xff0c;还会有相当一部分存储在计算机的内存中&#xff0c;这部分数据访问速度要快于数据库的访问&#xff0c;因此通常在做提升数据访问速度时&#xff0c;会将需要提升访问速度的数据放入到内存中&#xff0c;…

LeetCode算法入门- Multiply Strings -day18

LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 “2”, num2 “3” Output: “6” Exampl…

Linux下查看版本及系统信息

一、查看Linux发行版本 [rootlocalhost ~]# cat /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m[rootlocalhost ~]# cat /etc/redhat-release CentOS release 6.8 (Final)二、查看Linux内核信息 [rootlocalhost ~]# uname -a Linux localhost.localdomain 2.6.32…

LeetCode算法入门- Search Insert Position -day19

LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array.…