• 深入理解java集合框架之---------Arraylist集合 -----添加方法


    Arraylist集合 -----添加方法

    1、add(E e) 向集合中添加元素

      

    /**
    	 * 检查数组容量是否够用
    	 * @param minCapacity
    	 */
    	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;
    				elementData = Arrays.copyOf(elementData, newCapacity);
    			}
    		}
    		
    	}
    /* (non-Javadoc)
    	 * 向ArrayList中添加元素
    	 * @see java.util.AbstractList#add(java.lang.Object)
    	 */
    	public boolean add(E e){
    		ensureCapacity(size+1);
    		elementData[size++] = e;
    		return true;
    	}
    

     2、add(int  index ,E e) 向集合的指定索引处添加元素

      

    /**
         * 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++;
        }
    

      3、addAll(Collection< ? extends E> c) 添加集合

     

    /* 
    	 * 添加元素集合
    	 * (non-Javadoc)
    	 * @see java.util.AbstractCollection#addAll(java.util.Collection)
    	 */
    	public boolean addAll(Collection<? extends E> c){
    		Object[] a = c.toArray();
    		int numNew = a.length;
    		ensureCapacity(size+numNew);
    		System.arraycopy(a, 0, elementData, size, numNew);
    		size+=numNew;
    		return numNew!=0;
    		
    	}
    

      4、addAll(int index, Collection<? extends E> c); 在指定索引出添加集合

      

    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;
        }
    

      

  • 相关阅读:
    (2)javascript的基本语法、数据结构、变量
    (1)认识javascript
    CSS 浅析position:relative/absolute定位方式
    jquery实现下拉框多选
    Vue.js not detected
    手机代理调试Charles Proxy和Fiddler
    render函数之jsx应用
    vue组件通信方式(多种方案)
    点击页面空白处地方,隐藏弹窗
    css圆角不圆和1px方案
  • 原文地址:https://www.cnblogs.com/hanxue112253/p/9537709.html
Copyright © 2020-2023  润新知