簡體   English   中英

如何通過二進制搜索搜索 ArrayList 中的任何值

[英]How can I search any Value in ArrayList via Binary Search

由於獲取鍵值,我在獲取包含許多索引的列表時遇到問題。 我的代碼中有錯誤。

我的城市 class 如下圖所示。

public class City implements Serializable{
    private String cityName;
    private String countryName;
    ...
}

我的 Arraylist 包含城市名稱及其國家名稱,如下所示。

Shanghai, China
...
...

由於 Arraylist 的大小非常大,如 50000,我應用二進制搜索來搜索列表中的任何字符或單詞。

我想搜索對大寫或小寫敏感的任何字符或單詞並檢索它們的索引。

我該怎么做這個過程?

代碼按照如下所示定義的字符、字符串或單詞運行。

示例示例

Enter the word of character which I want to search : W
Enter the word of character which I want to search : Sha
Enter the word of character which I want to search : Shanghai

這是我的代碼片段,如下所示。

Scanner scanner = new Scanner(System.in);
System.out.print("Enter the word of character which I want to search : ");
String charWord = scanner.nextLine();
System.out.println("Search " + charWord);
Integer[] index = BinarySearch.binarySearch(cities, charWord);
System.out.println(index.toString());


public static Integer[] binarySearch( ArrayList<City> list, String key ) {
        Comparable comp = (Comparable)key;
        List<Integer> arrlist = new ArrayList<Integer>();
        Integer arr[] = null;
        int res = -1, min = 0, max = list.size() - 1, pos;
        while( ( min <= max ) && ( res == -1 ) ) {
            pos = (min + max) / 2;
            int comparison = comp.compareTo(key.contains(list.get(pos).getCityName()));
            if( comparison == 0) {
                res = pos;
                arrlist.add(res);
            }
            else if( comparison < 0)
                max = pos - 1;
            else
                min = pos + 1;
        }

        return arrlist.toArray(arr);
    }

這將使用城市名稱搜索

    private static class City {

        private String country;
        private String cityName;

        public City(String cityName, String country) {
            this.cityName = cityName;
            this.country = country;
        }

        public void setCityName(String cityName) {
            this.cityName = cityName;
        }

        public String getCityName() {
            return cityName;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        @Override
        public String toString() {
            return cityName;
        }
    }

    public static void main(String[] args) {
        List<City> list = new ArrayList<>();
        list.add(new City("Shanghai", "Shanghai"));
        list.add(new City("USA", "USA"));
        list.add(new City("China", "China"));
        list.add(new City("Germany", "Germany"));
        list.add(new City("China", "China"));
        list.add(new City("china", "china"));
        Integer[] indices = binarySearch(list, "China");
        for (int i = 0; i < indices.length; i++) {
            System.out.print(indices[i] + " ");
        }
        System.out.println();
    }

    public static Integer[] binarySearch(List<City> cities, Comparable key) {
        List<Integer> arrList = new ArrayList<Integer>();
        int lo = 0, hi = cities.size() - 1, mid;
        cities.sort((str1, str2) -> str1.getCityName().compareTo(str2.getCityName()));
        System.out.println(cities);
        while (lo <= hi) {
            mid = lo + (hi - lo) / 2;
            int cmp = key.compareTo(cities.get(mid).getCityName());
            if (cmp == 0) {
                arrList.add(mid);
                lo = mid + 1;
            } else if (cmp < 0)
                hi = mid - 1;
            else
                lo = mid + 1;
        }
        return arrList.stream().toArray(Integer[]::new);
    }

    private static Integer[] searchByCharacters(Integer[] indices, List<City> list, String sub) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getCityName().contains(sub))
                result.add(i);
        }
        return result.stream().toArray(Integer[]::new);
    }

, output

[China, China, Germany, Shanghai, USA, china]
0 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM