java - Java:在定制的通用向量中间插入元素
问题描述
我正在尝试编写代码以将字符串/整数/空值插入到 JAVA 中的通用向量的中间。但这部分代码无法编译。可能是什么问题?谢谢你。我还添加了错误消息。
public synchronized void addToPosition (T element, int index) {
if (index+1 == size-1) {
ensureCapacity();
}
System.out.println("Add element to position:");
Scanner scanner = new Scanner(System.in);
index = scanner.nextInt();
if (index < 0 || index > this.index-1) {
throw new IndexOutOfBoundsException();
}
T[] newElementData = (T[]) new Object [size];
for (i = 0; i < index; i++) {
newElementData[i] = elementData[i];
}
System.out.println("Which element to add?");
element = (T) scanner.nextLine();
newElementData[index] = element;
for (i = index+1; i < this.index; i++) {
newElementData[i] = elementData[i+1];
}
elementData = newElementData;
this.index++;
}
解决方案
正如您在评论中所说,您addToPosition()
像这样调用该方法:
vectorList.addToPosition();
而方法签名如下:
public synchronized void addToPosition (T element, int index) {
所以他们不匹配。例如,您可以摆脱参数:
public synchronized void addToPosition () {
T element; // the values you will read later from user input
int index;
推荐阅读
- azure-devops - 在 Azure Devops 中编写 EF6 迁移脚本
- c# - 如何从 HTTP POST 请求更改为 HTTPS POST 请求?
- excel - 影响 VBA 代码中的某些列/列范围
- c# - 如何在二进制 PowerShell 模块中扩展路径?
- html - 注册表单的小帮助框
- node.js - Passport 使用 bcrypt 对带有加密用户名的中间件进行身份验证
- javascript - 如何使用Javascript在数组内的嵌套元素中对字符串进行切片?
- php - 用户存款后余额未更新
- css - Material-UI中的AppBar,Toolbar使用哪个高度?
- reactjs - React TS - 你可以为 map() 分配默认值吗?