猿问

返回数组中以传递的初始值开头的字符串

我有一个 .txt 文件,其中包含状态数据,如下所示:


AL,Alab,4860

AK,Alas,7415

AZ,Ariz,6908

AR,Arka,2988    

我做了一个函数,它计算有多少个状态从初始传递开始:


public int CInitial(char initial) {

        int total = 0;

        for(int i = 0; i < states.length; i++) { //states is an array which includes all states present in the .txt file

        String testString = states[i].getName(); // getName gets the name of the states present in the .txt file

        char[] stringToCharArray = testString.toCharArray();

        for (char output : stringToCharArray) {

            if(initial == output) {

                total++;        

            }


        }   


    }

        return total; 

}

如果通过“A”,则返回数字 4,如果通过任何其他首字母,则返回数字 0,因为有 4 个状态以字母“A”开头。


现在我如何创建一个新函数来传递一个字符并返回以该字符开头的所有状态的名称?例如,这是为此所需的初始返回类型,但是我在开始时遇到了麻烦。该过程是否与我创建的countStatesCountByInitial函数相同?


public State[] CByInitial(char initial) {

        return new State[] {}; //to be completed    

    }   


缥缈止盈
浏览 173回答 2
2回答

心有法竹

是的,它将与countStatesCountByInitial. 主要区别在于每次找到匹配项时,您都希望将状态添加到数组中。由于我们事先不知道数组的大小,因此我们可能想使用 aList代替。public State[] getStatesCountByInitial(char initial) {&nbsp; &nbsp; ArrayList<State> found = new ArrayList<>();&nbsp; &nbsp; // this is the same as before&nbsp; &nbsp; for(int i = 0; i < states.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; String testString = states[i].getName();&nbsp; &nbsp; &nbsp; &nbsp; char[] stringToCharArray = testString.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; for (char output : stringToCharArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(initial == output) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // except here when you find a match, you add it into the list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found.add(states[i]);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; // return as array&nbsp; &nbsp; return found.toArray(new State[found.size()]);}正如帕特里克所建议的,我们可以List通过使用countStatesCountByInitial来初始化状态的大小来避免使用。public State[] getStatesCountByInitial(char initial) {&nbsp; &nbsp; int matchSize = countStatesCountByInitial(initial);&nbsp; &nbsp; States[] found = new States[matchSize];&nbsp; &nbsp; int foundIndex = 0;&nbsp; &nbsp; // this is the same as before&nbsp; &nbsp; for(int i = 0; i < states.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; String testString = states[i].getName();&nbsp; &nbsp; &nbsp; &nbsp; char[] stringToCharArray = testString.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; for (char output : stringToCharArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(initial == output) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // except here when you find a match, you add it into the array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found[foundIndex] = states[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foundIndex++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; // return the array&nbsp; &nbsp; return found;}&nbsp;

FFIVE

您可以通过一种方法简单地完成这两种操作。public static ArrayList<State> getStatesCountByInitial(char initial) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList selectedStates = new ArrayList<State>();&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < states.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(states.charAt(0) == initial){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedStates.add(states[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return selectedStates;}此方法将返回一个数组列表。如果要获取计数,则调用此方法并获取数组的大小。ArrayList<State> statesNew = getStatesCountByInitial('A');int count = statesNew.size();
随时随地看视频慕课网APP

相关分类

Java
我要回答