14. 编写函数 insertList

本文介绍了一个简单的Python函数insertList,该函数能够将用户输入的整数按大小顺序插入已排序的列表中。通过使用Python内置的list方法实现,确保列表在插入新元素后依然保持升序排列。

主程序中已有一个排好序的列表,请编写函数 insertList ,将从键盘接收的整数按原来从小到大的排序规律插入到该列表中。
def insertList(L1,x):
    #函数代码
L1=[1,4,6,9,13,16,28,40,100]
x=int(input(' 请输入一个要插入的整数: '))
insertList(L1,x)
print(L1)

一、思路:

主要考查list的方法,比如插入。

 

二、代码:

def insertList(L1, x):
    if x > L1[len(L1)-1]:
        L1.append(x)
    for i in range(0, len(L1)):
        if x < L1[i]:
            L1.insert(i, x) # 这就是list的舒服,直接可以插入指定序号前,不用考虑移位等等
            break
    return
L1 = [1, 4, 6, 9, 13, 16, 28, 40, 100]
x = int(input(' 请输入一个要插入的整数: '))
insertList(L1, x)
print(L1)

 

三、输出:

 请输入一个要插入的整数: 101
[1, 4, 6, 9, 13, 16, 28, 40, 100, 101]

 

 请输入一个要插入的整数: 2
[1, 2, 4, 6, 9, 13, 16, 28, 40, 100]

### Java 中 `insertList` 函数的实现方式 在 Java 中,虽然标准库并未提供名为 `insertList` 的内置方法,但可以通过自定义逻辑来实现类似功能。以下是基于常见的需求场景——向另一个列表中的指定位置插入一个子列表的内容。 #### 自定义 `insertList` 方法 下面是一个简单的例子,展示如何通过编写自定义方法将一个列表的部分数据插入到目标列表的特定索引处: ```java import java.util.ArrayList; import java.util.List; public class InsertListExample { public static void main(String[] args) { List<Integer> targetList = new ArrayList<>(); targetList.add(1); targetList.add(2); targetList.add(3); List<Integer> sourceList = new ArrayList<>(); sourceList.add(1001); sourceList.add(1002); sourceList.add(1003); sourceList.add(1004); int indexToInsertAt = 1; // 插入的目标索引 insertList(targetList, sourceList, indexToInsertAt); System.out.println("After insertion: " + targetList); // 输出结果验证 } /** * 将 sourceList 的所有元素插入到 targetList 的指定索引位置。 * * @param targetList 被操作的目标列表 * @param sourceList 需要插入的数据源列表 * @param index 插入的位置索引 */ public static <T> void insertList(List<T> targetList, List<T> sourceList, int index) { if (index < 0 || index > targetList.size()) { throw new IndexOutOfBoundsException("Index out of bounds"); } targetList.addAll(index, sourceList); // 使用 addAll 方法完成批量插入 } } ``` 上述代码展示了如何利用 `addAll(int index, Collection<? extends E> c)` 方法,在目标列表的某个索引位置插入来自另一列表的所有元素[^1]。 #### 关于迭代器的应用 如果需要逐个处理并手动控制插入过程,则可以借助 `ListIterator` 来遍历和修改列表内容。例如: ```java import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class IteratorInsertionExample { public static void main(String[] args) { List<Integer> targetList = new ArrayList<>(); targetList.add(1); targetList.add(2); targetList.add(3); List<Integer> sourceList = new ArrayList<>(); sourceList.add(1001); sourceList.add(1002); sourceList.add(1003); sourceList.add(1004); int indexToInsertAt = 1; ListIterator<Integer> iterator = targetList.listIterator(indexToInsertAt); for (Integer value : sourceList) { iterator.add(value); // 在当前位置逐一插入新元素 } System.out.println("After manual insertion with iterator: " + targetList); } } ``` 此示例中使用了 `listIterator()` 和循环调用其 `add(E e)` 方法,逐步完成了相同的功能[^2]。 #### 总结 无论是直接调用集合框架的方法还是采用更底层的手动管理策略,都可以灵活应对不同复杂度的需求。以上两种方案分别代表了高效简洁以及细粒度可控性的实现路径。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值