为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

缥缈止盈 2021-08-25 16:41:21
我有一个 .txt 文件,其中包含状态数据,如下所示:AL,Alab,4860AK,Alas,7415AZ,Ariz,6908AR,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        }   
查看完整描述

2 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

是的,它将与countStatesCountByInitial. 主要区别在于每次找到匹配项时,您都希望将状态添加到数组中。由于我们事先不知道数组的大小,因此我们可能想使用 aList代替。


public State[] getStatesCountByInitial(char initial) {

    ArrayList<State> found = new ArrayList<>();


    // this is the same as before

    for(int i = 0; i < states.length; i++) {

        String testString = states[i].getName();

        char[] stringToCharArray = testString.toCharArray();

        for (char output : stringToCharArray) {

            if(initial == output) {

            // except here when you find a match, you add it into the list

            found.add(states[i]);        

            }

        }   

    }


    // return as array

    return found.toArray(new State[found.size()]);

}

正如帕特里克所建议的,我们可以List通过使用countStatesCountByInitial来初始化状态的大小来避免使用。


public State[] getStatesCountByInitial(char initial) {

    int matchSize = countStatesCountByInitial(initial);

    States[] found = new States[matchSize];

    int foundIndex = 0;


    // this is the same as before

    for(int i = 0; i < states.length; i++) {

        String testString = states[i].getName();

        char[] stringToCharArray = testString.toCharArray();

        for (char output : stringToCharArray) {

            if(initial == output) {

                // except here when you find a match, you add it into the array

                found[foundIndex] = states[i];

                foundIndex++;

            }

        }   

    }


    // return the array

    return found;


查看完整回答
反对 回复 2021-08-25
?
FFIVE

TA贡献1797条经验 获得超6个赞

您可以通过一种方法简单地完成这两种操作。


public static ArrayList<State> getStatesCountByInitial(char initial) {

        ArrayList selectedStates = new ArrayList<State>();

        for(int i = 0; i < states.length; i++) {

            if(states.charAt(0) == initial){

                selectedStates.add(states[i]);

            }

        }

        return selectedStates;

}

此方法将返回一个数组列表。如果要获取计数,则调用此方法并获取数组的大小。


ArrayList<State> statesNew = getStatesCountByInitial('A');

int count = statesNew.size();


查看完整回答
反对 回复 2021-08-25
  • 2 回答
  • 0 关注
  • 172 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信