我正在尝试编写一个代码,以将字符串/整数/null插入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()
方法称为so:
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;