小编Lei*_*ghA的帖子

在数组列表中搜索最常见的String

我想知道如何搜索字符串的ArrayList以找到我创建的"行程"对象中最常出现的"目的地"(其中包含不同目的地的列表.)

到目前为止,我有:

public static String commonName(ArrayList<Itinerary> itinerary){

    int count = 0;
    int total = 0;

    ArrayList<String> names = new ArrayList<String>();
    Iterator<String>itr2 = names.iterator();

    while(itr.hasNext()){ 

        Itinerary temp = itr.next();  

        if(temp.iterator().hasNext()){ //if its has destinations

                // Destination object in itinerary object 
                Destination temp2 = temp.iterator().next(); 
                String name = temp2.getDestination().toLowerCase().replace(" ", "");

                if(names.contains(name)){
                    count = count + 1;
                    //do something with counting the occurence of string name here
                }
Run Code Online (Sandbox Code Playgroud)

我在制作一个算法来搜索数组中最常出现的字符串时遇到问题,或者如果存在平局就会出现字符串; 然后显示找到字符串的'Itinerary对象'(参数值)的编号.任何帮助都会很棒,谢谢!

java

4
推荐指数
1
解决办法
4082
查看次数

标签 统计

java ×1