1
A
回答
2
- 使用二进制搜索(如果这是一个链表,可能是相当昂贵的迭代),以寻找到新的项目属于
- 的位置,如果该值是相同的 - 什么都不做
- 如果值是不同的,需要在这里插入,这意味着将所有的事情从这个位置移回到一个结尾(如果这是一个链表,这意味着在这一点插入一个新节点,不必做所有的移动)
- 将新项目插入索引。
1
假设您使用的是静态数组,例如没有链表
以下是一种方式做字符串数组,你可以定制按您的要求
//与项目的有序列表 的String [] sortedArray =新的String [] {“蚂蚁创建anarray “,”蝙蝠“,”猫“,”狗“};
// Search for a non-existent item and then insert it
int index = Arrays.binarySearch(sortedArray, "cow");
if (index < 0) {
// Compute the insert index
int insertIndex = -index-1;
// Insert the new item into sortedArray. The example here creates
// a new larger array to hold the new item.
String[] newSortedArray = new String[sortedArray.length+1];
System.arraycopy(sortedArray, 0, newSortedArray, 0, insertIndex);
System.arraycopy(sortedArray, insertIndex,
newSortedArray, insertIndex+1,
sortedArray.length-insertIndex);
newSortedArray[insertIndex] = "cow";
sortedArray = newSortedArray;
}
参考http://www.exampledepot.com/egs/java.util/coll_InsertInArray.html
相关问题
- 1. 递归插入排序列表
- 2. 插入排序 - 链接列表 - 递归
- 3. java - 排序列表递归插入方法
- 4. java插入排序递归
- 5. 在排序列表中递归插入,删除检索
- 6. 递归在python,如插入排序
- 7. 建立/插入到排序列表中
- 8. 以递归方式在Java中找到数组中最长的递增序列
- 9. 阵列在数组上递归插入排序出界
- 10. 递归识别排序列表
- 11. 递归排序文件列表(linux)
- 12. Java链接列表递归插入
- 13. 排序阵列递归
- 14. 以递归方式删除列表中的空格(列表中的列表...)python
- 15. SQLAlchemy排序列表插入
- 16. 插入排序列表
- 17. 以序列方式插入新行
- 18. 递归排列
- 19. PHP:填充阵列以递归方式
- 20. 以排序格式插入链接列表
- 21. 如何在sml/nj中以非递归方式展平列表?
- 22. 我如何可以递归插入Fibonacci序列为二叉树
- 23. 在python中使用递归函数进行插入排序
- 24. 以递归方式在Python中找到列表中第k个最大的int
- 25. 得到表中的值以递归方式
- 26. 在python中递归排列排列
- 27. 递归符号样式有序列表
- 28. 序言:选择不在列表递归期间插入元素
- 29. 排序列表 - pythonic方式
- 30. 将值插入递归表中?
是它功课再加入功课标签? – 2012-02-09 10:13:47