list不通过add方法添加元素,用什么方法
来源:互联网 发布:mac 安装anywhere 编辑:程序博客网 时间:2024/04/27 16:59
public interface List<E> extends Collection<E>是接口,不能直接使用,必须由它的实现子类才能使用。
set(index, obj);方法某些时候可以实现添加新元素。
以下是ArrayList的set源码部分:
/**
* Replaces the element at the specified position in this list with
* the specified element.
*使用element替代list中指定位置的element
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
RangeCheck(index);//边界查询,判断index是否越界
E oldValue = (E) elementData[index];//取出index位置的元素
elementData[index] = element;//替换指定位置的元素
return oldValue;//返回被替换的元素
}
set方法的含义:用指定的元素替代此列表中指定位置上的元素。如果指定位置是为空,那么就相当于add(int index, E element)方法,添加新元素到list中,如果这个index位置上已经存在值,那么只能是替换原有值。
阅读全文
0 0
- list不通过add方法添加元素,用什么方法
- List使用add方法添加对象问题
- list add方法
- List add方法覆盖
- List使用add方法添加数据时的覆盖问题
- List使用add方法添加数据时的覆盖问题
- List不通过foreach查找元素
- List.add()方法中的陷阱
- java list add 方法理解
- List,Map,Set add方法
- List的add方法剖析
- list add方法的误区
- List的add方法剖析
- list add set remove方法
- list add方法学习总结
- python中List添加元素的几种方法
- python中List添加,删除元素的几种方法
- python中List添加、删除元素的几种方法
- 起源
- python实现邮件发送
- FPGA中状态机的稳定性
- Java内部类
- 计算圆形和长方形的面积
- list不通过add方法添加元素,用什么方法
- MarkDown语法记录
- linux学习第二十三篇:shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向
- Dubbo源码分析-config
- 代码练习
- Qt一些鲜为人知的小功能
- Django 快速搭建博客 第六节
- 数据结构实验之二叉树一:树的同构
- 1025. 反转链表 (25)