ArrayList / Vector
Object[] elementData
默认容量10
Object[] Arrays.copyOf(Object[] original, int newLength)
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
ArrayList 扩容1.5倍
Vector 扩容2倍
// capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。 Vector(int capacity) // capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。 Vector(int capacity, int capacityIncrement)
Vector MyVector = new Vector(100,50);(
创建一个数组类对象MyVector,有100个元素的空间,若空间使用完时,以50个元素空间单位递增
1、为什么要了解Java 集合类
Java 集合类提供了如线性表,链表和哈希表等基础数据结构的实现,通过它可以实现各种你想要的数据结构,如stack ,queue 等,了解Java 集合类的工作原理可以编出更高效性能的程序,另外了解其工作原理可以更好地使用它,避免因为滥用而出现性能上的问题。事实证明,很多性能上的问题都是因为使用不当引起的。
2、Java collection 的继承关系
Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
└SortedSet
└HashSet
└TreeSet
Map
├Hashtable
├HashMap
├SortedMap
└TreeMap
└WeakHashMap
1、Vector
Vector比ArrayList多了个protected int capacityIncrement; 属性
扩容方法:
/** * 核心方法之一:保证数组的容量,如果没有指定容量的增长的步长,则将原数组容量扩 * 大成原来的两倍,如果指定则按照增长的步长增加容量,并且取 minCapacity 和 *newCapacity 计算出来的最大值进行取值。计算出来的最终容量将作为新数组对象的尺 * 寸,并将原来数组的元素 copy 到新数组里面来。 * * @see #ensureCapacity(int) */ private void ensureCapacityHelper( int minCapacity) { int oldCapacity = elementData . length ; if (minCapacity > oldCapacity) { Object[] oldData = elementData ; int newCapacity = ( capacityIncrement > 0) ? (oldCapacity + capacityIncrement ) : (oldCapacity * 2); if (newCapacity < minCapacity) { newCapacity = minCapacity; } elementData = Arrays.copyOf ( elementData , newCapacity); } }
Vector 使用时需要注意的问题
1) Vector 使用时,最好能够预期需要存放元素的个数,这样可以避免数组长度频繁的改变而引起数据的 copy 带来的开销,同时会引起内存大小的增加。
2 )使用 Vector 需要知道 Vector 是线程安全的。因此,在使用时,最好是考虑这个对象会不会因为多个线程访问该 Vector 对象,如果确认只有单个线程访问,则可以用 ArrayList 来代替 Vector 。
2、ArrayList:
public void ensureCapacity(int minCapacity) { modCount++; int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } }
Vector和ArrayList在使用上非常相似,都可用来表示一组数量可变的对象应用的集合,并且可以随机地访问其中的元素。
Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),
ArrayList的方法不是,由于线程的同步必然要影响性能,因此,ArrayList的性能比Vector好。
当Vector或ArrayList中的元素超过它的初始大小时,Vector会将它的容量翻倍,而ArrayList只增加50%的大小,这样,ArrayList就有利于节约内存空间。
Arrays.copyOf(elementData, size)
/**<Object> Object[] java.util.Arrays.copyOf(Object[] original, int newLength) Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array. Parameters: original the array to be copied newLength the length of the copy to be returned Returns: a copy of the original array, truncated or padded with nulls to obtain the specified length */
Arrays.copyOf(elementData, size, Object[].class)
/**Parameters: original the array to be copied newLength the length of the copy to be returned newType the class of the copy to be returned */
System.arraycopy(elementData, 0, a, 0, size)
/** void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array. If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array. Parameters: src the source array. srcPos starting position in the source array. dest the destination array. destPos starting position in the destination data. length the number of array elements to be copied. */
几个主要方法:
public Object[] toArray() { return Arrays.copyOf(elementData, size); }
/** * Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { if (index > size || index < 0) throw new IndexOutOfBoundsException( "Index: "+index+", Size: "+size); ensureCapacity(size+1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; }
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { RangeCheck(index); modCount++; E oldValue = (E) elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work return oldValue; }
/** * Appends all of the elements in the specified collection to the end of * this list, in the order that they are returned by the * specified collection's Iterator. The behavior of this operation is * undefined if the specified collection is modified while the operation * is in progress. (This implies that the behavior of this call is * undefined if the specified collection is this list, and this * list is nonempty.) * * @param c collection containing elements to be added to this list * @return <tt>true</tt> if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacity(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; }
/** * Inserts all of the elements in the specified collection into this * list, starting at the specified position. Shifts the element * currently at that position (if any) and any subsequent elements to * the right (increases their indices). The new elements will appear * in the list in the order that they are returned by the * specified collection's iterator. * * @param index index at which to insert the first element from the * specified collection * @param c collection containing elements to be added to this list * @return <tt>true</tt> if this list changed as a result of the call * @throws IndexOutOfBoundsException {@inheritDoc} * @throws NullPointerException if the specified collection is null */ public boolean addAll(int index, Collection<? extends E> c) { if (index > size || index < 0) throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); Object[] a = c.toArray(); int numNew = a.length; ensureCapacity(size + numNew); // Increments modCount int numMoved = size - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); size += numNew; return numNew != 0; }
相关推荐
11KW OBC两电平pfc+cllc仿真源码实现:单相与三相兼容版双向控制研究,11KW OBC两电平pfc+cllc仿真源码实现:单相与三相兼容版,实现双向控制策略,11KW OBC两电平pfc+cllc仿真,源代码实现。 注意:已成单相,三相兼容版仿真文件。 双向控制。 ,核心关键词:11KW OBC两电平pfc; CLLC仿真; 源代码实现; 单相三相兼容; 双向控制。,11KW OBC单相与三相兼容版仿真:两电平PFC+CLLC双向控制源代码实现
3GPP R15 38.331 5G NR无线资源控制(RRC)协议规范解析
五运六气YUNQI_V471_SRC_D1023
19考试真题最近的t63.txt
基于MATLAB的牛拉法电力系统潮流计算程序,结合BPA方法,附参考文献,适合基础学习与拓展创新,基于MATLAB的牛拉法电力系统潮流计算程序:涵盖基础学习与拓展创新,附参考文献,牛拉法电力系统潮流计算 MATLAB编写潮流计算程序 BPA计算潮流 另外包含参考文献 这个程序把潮流计算的一般流程包括了,非常适合基础学习,并进一步的进行拓展创新 ,牛拉法; 电力系统潮流计算; MATLAB; BPA计算; 程序编写; 流程; 基础学习; 创新拓展,基于MATLAB的牛拉法电力系统潮流计算程序:基础学习与拓展创新指南
YOLOv11m权重文件
高一-语文-2025年1月张家界市高一期末联考-缺考不计、违纪不计、0分不计_2025-01-16-12-21 (1).zip
android kotlin 版本的贪吃蛇游戏
19考试真题最近的t57.txt
基于疫情封控区域的生活物资配送优化模型:结合遗传算法与模拟退火,实现时间最短和综合满意率最高的路径优化。,疫情下封控区域生活物资配送优化模型:结合遗传算法与模拟退火算法求解路径优化问题,实现时间与满意率双重目标优化。,模型及MATLAB代码:考充分考虑并结合疫情下封控区域生活物资配送问题及车辆路径问题的特点构建物资配送优化模型。 在一般单一目标——时间最短的基础上,加入综合满意率优化目标的路径优化问题 关键词:遗传算法、改进、模拟 火算法,路径优化、CVRP 完整模型+代码+注释 主要内容:以配送时间最短及综合满足率最高为目标,充分考虑并结合疫情下封控区域生活物资配送问题及车辆路径问题的特点构建物资配送优化模型,为疫情下生活物资配送找到了更好的思路。 在模型设计与求解问题上,首先设计标准遗传算法,继而对算法加以改进,最后设计出了改进遗传-模拟 火算法对模型进行求解。 还有参数灵敏度分析等。 服务内容:脚本 工具 部分展示如下: ,关键词:疫情下物资配送;车辆路径问题;优化模型;遗传算法;改进;模拟退火算法;CVRP;参数灵敏度分析;脚本工具;时间最短;综合满意率。 核心关键词用分号分
## 01、数据介绍 动态能力理论最早由提斯(Teece)与皮萨洛(Pisano)于1994年正式提出,他们将动态能力定义为“能够创造新产品和新过程,以及对变化的市场环境做出响应的一系列能力”。 动态能力具体体现在吸收能力、创新能力和适应能力三个方面。这些能力使公司能够快速适应市场变化,抓住新的商业机会,从而保持或提升竞争优势。 数据名称:上市公司-动态能力数据 数据年份:2012-2023年 ## 02、相关数据及指标 证券代码 证券名称 年份 Symbol RD IA ACV DC
基于ASIO的插件式服务器,支持TCP, UDP, 串口,Http, Websocket,统一化的数据接口,隔离开发人员和IO之间的操作。可以快速迭代。PSS 是针对不同 IO 逻辑的插件管理系统。您可以忽略 IO 建立的细节,构建自己的 logic 应用程序。PSS 封装了 Tcp、udp、kcp、串行端口、http、websocket 和 ssl 的统一接口。您可以使用 配置文件 或 统一接口 来创建和使用它们。logic plug-in 是完成数据到达后的 logic 处理,全部以动态库的形式加载,将 IO 和 logic 本身的耦合分开。简单的逻辑开发。本项目由三部分组成 (1) 主机(2) 数据包分析插件(3) 逻辑处理插件。您可以实现后两个插件来完成您的业务逻辑部署。
电机控制器源码:通用无感BLDC方波控制,高效参数化启动,转速达12w,环控系统一键调节,电机控制器源码:通用无感BLDC方波控制,高效调速,参数宏定义便捷调试,最高电转速达12w,电机控制器,低压无感BLDC方波控制,全部源码,方便调试移植 1.通用性极高,图片中的电机,一套参数即可启动。 2. ADC方案 3.电转速最高12w 4.电感法和普通三段式 5.按键启动和调速 6.开环,速度环,限流环 7.参数调整全部宏定义,方便调试 代码全部源码 ,电机控制器;低压无感BLDC方波控制;全部源码;通用性极高;电转速最高12w;电感法与普通三段式;按键启动调速;开环、速度环、限流环;参数调整宏定义。,通用电机控制器:低压无感BLDC方波控制源码,支持高转速12W,便捷调试移植
基于MPC的电动汽车分布式协同自适应巡航控制:上下分层控制与仿真结果展示,基于MPC的电动汽车协同自适应巡航控制:上下分层控制与仿真结果展示,基于MPC的分布式电动汽车协同自适应巡航控制,采用上下分层控制方式,上层控制器采用模型预测控制mpc方式,产生期望的加速度,下层根据期望的加速度分配扭矩;仿真结果良好,能够实现前车在加减速情况下,规划期望的跟车距离,产生期望的加速度进行自适应巡航控制。 ,关键词:MPC(模型预测控制); 分布式电动汽车; 协同自适应巡航控制; 上下分层控制方式; 期望加速度; 扭矩分配; 仿真结果良好; 前车加减速; 跟车距离。,基于MPC的分层控制电动汽车自适应巡航系统,仿真实现前车加减速跟车距离自适应
多维度购电与售电模型构建及基于CVaR与WOA优化的收益风险评估方法研究,基于CVaR风险评价及WOA优化的新型售电公司购售电模型与策略仿真研究,建立了一个电公司的购电侧模型和电侧模型,其中购电侧模型包括长期市场业务,现市场业务,可再生能源购电市场业务,分布式电源购电市场业务以及储能租赁市场业务五种类型。 电侧包括均 电价合同和实时电价合同两种类型。 然后在购电模型的基础上,提出了一种基于CVaR的购电收益风险评价方法。 根据电公司的CVaR购电收益风险数学模型,提出了一种基于WOA优化算法的新型购电收益计算方法。 该方法将购电收益风险计算公式作为WOA优化算法的目标函数,然后通过WOA的鲸鱼行走觅食、鲸鱼包围以及鲸鱼螺旋捕食三个步骤计算电公司的最优购电策略。 最后,通过MATLAB仿真工具对本文所研究的基于WOA优化的新型购电收益计算方法进行了仿真分析。 仿真结论验证了通过WOA优化算法得到的购电策略为最优购电策略。 matlab代码 仿真平台:MATLAB平台 代码具有一定的深度和创新性,注释清晰 ,关键词: 1. 购电侧模型; 2. 售电侧模型; 3. 长期/现货/可再生
迅雷软件下载原理介绍.md
## 01、数据简介 碳排放是指在人类活动中,如能源消耗、工业生产、交通运输、农业活动等过程中向大气中释放的二氧化碳等温室气体的行为。这些温室气体在大气中形成隔热层,导致地球气温升高,引发全球气候变化。分行业碳排放则是指按照不同的经济活动或产业部门来划分和统计碳排放量。 按省市县整理成面板数据,其中包括电力行业、工业过程、工业燃烧、建筑物能源、浪费、农业、燃料能源和运输八种指标排放量各省市县的最大值、最小值、平均值、总和。 数据名称:省市县分行业碳排放月度数据 数据年份:2023年 ## 02、相关数据 name 指标 时间 数值 更多数据 ## 03、数据截图
基于OpenCV 相机校准 姿势估计 线性几何 立体图像的深度图
电力系统潮流计算标准算例库:涵盖多种格式与节点拓扑图的数据集(从3节点至300节点),电力系统潮流计算标准算例库:涵盖多种格式与节点拓扑图的数据集(从3节点至300节点全量收录),电力系统潮流计算标准算例的数据(从3节点到300节点都齐了)。 包含IEEE格式、BPA格式、清华格式,同时有各个节点的拓扑图 ,关键词:电力系统;潮流计算;标准算例;数据;节点;IEEE格式;BPA格式;清华格式;拓扑图,电力系统多节点潮流计算标准算例数据及拓扑图解析