java - 使用不太具体的参数调用重载方法

标签 java

java.util.Vector 有方法: 删除(int索引)删除(对象o)

我有:

vector<Integer> a;
int b=3;

我想要:

使用 b 变量调用方法 remove(Object o)。 编写 a.remove(b) 显然会调用 remove(int index)

提前致谢,Maciej

最佳答案

a.remove(Integer.valueOf(b)); 

应该可以。在自动装箱被认为调用 remove(int) 之前,Integer 将首先被解析为引用类型,并匹配 remove(Object)

来自Java Language Spec, 15.2.2 :

Compile-Time Step 2: Determine Method Signature

The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing...

关于java - 使用不太具体的参数调用重载方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15417512/

相关文章:

java - hibernate nextval 获取 id

java - 如何使用mysql在jtable中显示Url Image?

java - 如何在 Java 中解析 "Event XML"?

java - AsyncTask在doInBackground中止

java - Mac selenium webdriver chrome 窗口总是以小窗口开头

java - jFreeChart 在 Android 2.x 中工作/完成吗

java - Spring Boot 和 Docker-compose 设置参数

java - 如何使数据成员只能被类和子类访问

java - 将文本文件内容逐行存储到数组中

java - Spring MVC 支持的 Freemarker 中基于枚举的单选按钮