Java Basis - container ArrayList

List class has two implementations: the ArrayList (underlying array implemented) and the LinkedList (underlying list is implemented)

The ArrayList : Because of its underlying implementation is an array, so that the majority of the methods associated with the operation in the target array. May have experience in this area for students interested in JDK source code, today we have its own way to implement a simple ArrayList (Note that, here and does not implement all methods, and certainly on the merits of the method is not enough to match the JDK source code), here it is mainly to let everyone understand ArrayList is not simply remain at the level of increase, delete, change of
look at the JDK source code in the method of the ArrayList (we can see very much its methods , but not all need to know in practical applications, we only need to understand a few important ways since then with ArrayList is quite handy)
Here Insert Picture Description
define two variables
Private Object [] elementData ;
Private int size ; (due to the ArrayList the underlying implementation is to define an array should Object type array of objects)

The first method: check whether the subscript legitimate

Code to realize the idea:
was introduced into a specific index, it is determined whether valid (or is negative than when the container is not legal length). Although its implementation is quite easy, but because many methods are based on the index operation, it is also very important.

private  void rangeCheck(int index) {
		if(index<0||index>=size) {
			try {
				System.out.println("越界了");
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

The second method: add an object to the container (end)

代码实现思路:添加某个元素时:
1.我们应该首先判断容器的大小,如果没有超过预先设置好的数组长度,那我们直接在数组的下一位增加一个值即可.
2.如果添加时,发现它的长度已经超过预先数组设置的长度时,我们就应该先对数组进行扩容。
1)扩容时,由于定义时数组的长度是定长。这时我们只能通过重新建立一个新数组,然后再把老数组复制到新数组上, 这就实现 了数组的扩容了。
2)在这里我们运用了System为我们封装好的数组复制方法**:public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)**

src:源数组
srcPos:从源数组的起始位置开始
dest:目标数组
destPos:目标数组的开始位置
length:要copy数组的长度

public void add(Object obj) {
		//数组扩容和数据的拷贝
		if(size==elementData.length) {
			Object[] newArray=new Object[size*2+2];
			System.arraycopy(elementData, 0, newArray, 0, elementData.length);
			elementData=newArray;
		}
		elementData[size++]=obj;
	}

第三个方法:给容器指定的为添加对象

代码的实现思路:给容器指定的位置添加对象时,
1)我们应该先进行插入的下标位置检查,如果添加操作时的长度没有超过预先设置好的数组长度,那么直接在指定位置添加即可,
2)如果超过指定长度,那我们应该进行数组扩容后,再把元素添加到指定的位置

public void add(int index,Object element) {
		if(index>size||index<0) {
			try {
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//数组扩容和数据的拷贝
		if(size==elementData.length) {
			Object[] newArray=new Object[size*2+2];
			System.arraycopy(elementData, 0, newArray, 0, elementData.length);
			elementData=newArray;
		}			
		System.arraycopy(elementData, index, elementData, index+1, size-index);
		elementData[index]=element;
		size++;
	}

Array copy performed when the above comparison code should have doubts or adding new elements: System.arraycopy (elementData of, index, elementData of,. 1 + index, index-size) , each parameter value is determined.

elementData of : represents the original array
index : from the data representing the start position of the index to be moved back (as it often is inserted into the lower subscript index)
elementData of : represents the original array
index +. 1 : represents the position of the newly started when move
size -index : indicates the length of the need to move

Its schematic diagram is as follows:
Here Insert Picture Description
Fourth Method: removing the target objects specified

Code to realize the idea: check whether the incoming index offside, then copy between arrays.
A schematic view to achieve the following:
Here Insert Picture Description

public void remove(int index) {
		rangeCheck(index);
		//删除指定位置的对象
		int numMoved=size-index-1;
		if(numMoved>0) {
			System.arraycopy(elementData, index+1, elementData, index, numMoved);
		}
		elementData[--size]=null;
	}

The fifth method: Remove the specified object

Code implementation ideas: to traverse the entire array, with equal () method to match the same elements can call the above encapsulated remove () method

public void remove(Object obj) {
		for(int i=0;i<size;i++) {
			if(get(i).equals(obj)) { //注意底层调用的是equal方法
				remove(i);
			}
		}
	}

The above method is to achieve five of the more commonly used, have a better idea if you can leave a message directly in the comments section.
The next section we will explain LinkedList

Published 19 original articles · won praise 2 · Views 424

Guess you like

Origin blog.csdn.net/TheWindOfSon/article/details/103131588