我有一个程序,它接受一个表示数组索引的数字和一个字符串来替换与索引对应的数组元素。
这是我的代码:
public static void main(String args[]){
String names[]={"Alvin", "Einstein", "Ace", "Vino", "Vince"};
for(int i=0; i<names.length;i++)
System.out.println(i + " - " + names[i]);
replaceArrayElement(names);
}
public static void replaceArrayElement(String name[]){
int index = 0;
String value = "";
Scanner scan = new Scanner(System.in);
System.out.print("\nEnter the index to be replaced:");
index = scan.nextInt();
if(index<name.length){
System.out.print("Enter the new value:");
value = scan.nextLine();
name[index] = scan.nextLine();
System.out.println("\nThe new elements of the array are");
for(int j=0; j<name.length;j++)
System.out.println(name[j]);
}
else{
System.out.println("Error!");
}
}
我需要做的是将 int 索引变量和 String 值变量作为参数放入方法 replaceArrayElement 中。但我不知道如何调用具有不同数据类型参数的方法。有人可以告诉我怎么做吗?谢谢你。