⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e362. inserting an element into a sorted list.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
This example demonstrates how to determine the index at which an element should be inserted into a sorted list. Although binarySearch() is used to locate existent elements, it can also be used to determine the insert index for non-existent elements. Specifically, the insertion index is computed in the following way: insert-index = (-return-value)-1 
    // Create a list with an ordered list of items
    List sortedList = new LinkedList();
    sortedList.addAll(Arrays.asList(new String[]{"ant", "bat", "cat", "dog"}));
    
    // Search for the non-existent item
    int index = Collections.binarySearch(sortedList, "cow");      // -4
    
    // Add the non-existent item to the list
    if (index < 0) {
        sortedList.add(-index-1, "cow");
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -