我需要重命名数组中的元素的帮助

时间:2018-11-21 20:29:35

标签: java arrays rename

我在学校正在做一个Java项目,我们遇到了以下问题:

  

重命名–更改现有工作表的名称

     

public int rename(String currentName, String newName)

     

rename传递了两个工作表名称。仅当currentName工作表名称在列表中而newName工作表名称不在列表中时,才可以重命名。否则rename不执行任何操作(即列表不受影响)。   如果currentName成功更改为newName,则该方法将返回已重命名的工作表的索引位置。否则,它将返回-1

这是我到目前为止所拥有的

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;
}

我得到了重命名部分,但是我不能不将其重命名为同名

4 个答案:

答案 0 :(得分:0)

遍历SheetName,并将currentName索引的任何实例存储在一个称为index的变量中,同时还要检查列表中是否存在newName。如果有newName的实例,则自动返回-1。但是,如果列表中没有newName,则currentName的索引实例将存储在index变量中(如果存在)。如果不存在,则该函数始终返回-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 :(得分:0)

这是解决方案。

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 class SheetNamesTest {

    private SheetNames sheetNames;

    @Before
    public void setup() {
        sheetNames = new SheetNames();
    }

    @Test
    public void shouldReplaceSheetName() {
        String[] sheetNamesArr = {"dennis", "john", "dan"};
        Assert.assertEquals(new Integer(1), sheetNames.changeSheetName(sheetNamesArr, "john", "peter"));

        String[] sheetNamesArr2 = {"john", "dan"};
        Assert.assertEquals(new Integer(0), sheetNames.changeSheetName(sheetNamesArr2, "john", "peter"));
    }

    @Test
    public void shouldNotReplaceSheetName() {
        String[] sheetNamesArr = {"dennis", "johnhunt", "dan"};
        Assert.assertEquals(new Integer(-1), sheetNames.changeSheetName(sheetNamesArr, "john", "peter"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowExceptionWhenSheetNameIsNull() {
        sheetNames.changeSheetName(null, "john", "peter");
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowExceptionWhenSheetNameIsEmpty() {
        String[] sheetNamesArr = {};
        sheetNames.changeSheetName(sheetNamesArr, "john", "peter");
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowExceptionWhenCurrentNameIsNull() {
        String[] sheetNamesArr = {"dennis", "johnhunt", "dan"};
        sheetNames.changeSheetName(sheetNamesArr, null, "peter");
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowExceptionWhenNewNameIsNull() {
        String[] sheetNamesArr = {"dennis", "johnhunt", "dan"};
        sheetNames.changeSheetName(sheetNamesArr, "john", null);
    }

}

答案 2 :(得分:0)

阅读下面的代码中的注释:

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;
}

答案 3 :(得分:-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