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

标签 java arrays rename

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

rename – changes the name of an existing sheet

public int rename(String currentName, String newName)

rename is passed two sheet names. Renaming should only be performed if the currentName sheet name is in the list and the newName sheet name isn’t. Otherwise rename does nothing (i.e. the list is unaffected). If the currentName is successfully changed to the newName then the method returns the index position of the sheet renamed. Otherwise it returns -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;
}

我得到了重命名部分,但我无法让它不重命名为同名的东西

最佳答案

这是解决方案。

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

}

关于java - 我需要帮助重命名数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53420031/

相关文章:

java - 使用二维数组查找列中的最大数字

R dplyr::使用字符串变量重命名并选择

c - 我可以通过哪些方式(使用 stdio)打印垂直直方图

php - 如何为数组中的每个子项添加父项 ID? php

variables - 生成多个样式表以 gulp

python - 读取 pandas csv 文件时如何重命名值

java - 在多台机器上运行 tomcat 时的 Quartz 集群设置

java - 为什么在一个字符串字面量之后,所有的+都会被当作字符串连接运算符?

java - Hibernate、MySQL 和 Maven : am I losing my configuration?

java - SimpleMatrix getMatrix().getData() 问题