Java 我需要帮助重命名数组中的元素
我在学校做一个java项目,我们给出了以下问题: 重命名–更改现有图纸的名称 public int renameString currentName,字符串newName 重命名将传递两个图纸名称。仅当currentName工作表名称在列表中而newName工作表名称不在列表中时,才应执行重命名。否则重命名将不起任何作用,即列表不受影响。 如果currentName成功更改为newName,则该方法返回重命名工作表的索引位置。否则返回-1 这就是我目前所拥有的Java 我需要帮助重命名数组中的元素,java,arrays,rename,Java,Arrays,Rename,我在学校做一个java项目,我们给出了以下问题: 重命名–更改现有图纸的名称 public int renameString currentName,字符串newName 重命名将传递两个图纸名称。仅当currentName工作表名称在列表中而newName工作表名称不在列表中时,才应执行重命名。否则重命名将不起任何作用,即列表不受影响。 如果currentName成功更改为newName,则该方法返回重命名工作表的索引位置。否则返回-1 这就是我目前所拥有的 public int rename
public int rename(String currentName, String newName) {
int i = 0;
for (i=0; i<SheetsNames.length; i++) {
if (SheetsNames[i].equals(currentName)) {
SheetsNames[i] = newName;
return i;
}
}
return -1;
}
我得到了重命名部分,但我无法让它不重命名为同名的东西在您的SheetsNames中循环并检查它是否已包含newName如果是,则您不想更新,因此返回-1
for(String name: SheetsNames){
if(name.equals(newName) return -1;
}
现在我们确定新名字不在名单上!让我们检查其中是否有与currentName相同的名称,如果是,则返回其索引
for(int i=0;i<SheetsNames.length;i++){
if(name.equals(currentName){
SheetsNames[i] = newName;
return i;
}
}
return -1; //its not in SheetsNames, so we return -1
循环浏览SheetName并将currentName索引的任何实例存储在名为index的变量中,同时检查列表中是否存在新名称。如果有newName的实例,则自动返回-1。但是,如果列表中没有新名称,则currentName索引的实例将存储在索引变量(如果存在)中。如果没有,那么函数将返回-1,因为索引初始化为-1
public int rename(String currentName, String newName) {
int index = -1;
for (int i=0; i<SheetsNames.length; i++) {
if (SheetsNames[i].equals(newName))
return -1;
if (SheetsNames[i].equals(currentName))
index = i;
}
if (index != -1)
SheetNames[index] = newName;
return index;
}
这是解决办法
1输入参数已验证
2在数组中查找字符串并返回索引
public class SheetNames {
public Integer changeSheetName(String[] sheetNames, String currentName, String newName) {
if (sheetNames == null || sheetNames.length == 0 || currentName == null || newName == null) {
throw new IllegalArgumentException("Invalid input");
}
Integer replacedIndex = Arrays.asList(sheetNames).indexOf(currentName);
if (replacedIndex != -1) {
sheetNames[replacedIndex] = newName;
}
System.out.println(Arrays.toString(sheetNames));
return replacedIndex;
}
}
Junit测试:-
请阅读以下代码中的注释:
public int rename(String currentName, String newName) {
int currentNameindex = -1; // set the initial position of currentName
for (int i = 0; i < SheetsNames.length; i++) {
if (currentNameindex < 0) // currentName has not been found yet
if (SheetsNames[i].equals(currentName))
currentNameindex = i; // save the position of currentName
if (SheetsNames[i].equals(newName)) // newName is found in the list
return -1;
}
if (currentNameindex > 0) // currentName is found in the list
SheetsNames[currentNameindex] = newName; // rename the list item
return currentNameindex;
}
如果currentName不在数组中,是什么让您认为它仍将执行某些操作?您是否使用未包含的元素对其进行了测试,并且它更改了一个元素?如果是这样,请在帖子中加入此场景如果newName.equalscurrentName{return-1;}?在一个很小的家庭作业案例中,这不会增加太多。什么是表格?从语法亮点和普通变量以小写开头这一事实可以看出,我假设您访问的是类型定义,而不是变量。但我无法让它不重命名为同名的对象。您如何一手执行正确的布尔运算,另一方面,我们找不到如何执行99%相似的布尔运算?SheetNames是列表还是数组,你提到了两者吗?进行检查的唯一一点是避免在所有成员之间循环。
public int rename(String currentName, String newName) {
int currentNameindex = -1; // set the initial position of currentName
for (int i = 0; i < SheetsNames.length; i++) {
if (currentNameindex < 0) // currentName has not been found yet
if (SheetsNames[i].equals(currentName))
currentNameindex = i; // save the position of currentName
if (SheetsNames[i].equals(newName)) // newName is found in the list
return -1;
}
if (currentNameindex > 0) // currentName is found in the list
SheetsNames[currentNameindex] = newName; // rename the list item
return currentNameindex;
}