1.手写ArrayList 实现其基本功能
/**
*
* 手写ArrayList 实现其基本功能
*
*/
public class SexArrayList {
private Object[] elementData;
private int size;
public SexArrayList(){
this(10);
}
//设置数组的空间
public SexArrayList(int initialCapacity){
if(initialCapacity<0){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
elementData = new Object[initialCapacity];
}
//添加元素
public void add(Object obj){
//数组扩容
if(size+1 >elementData.length){
Object[] newArray = new Object[size*2+1];
System.arraycopy(elementData, 0, newArray, 0, elementData.length);
}
elementData[size++] = obj;
}
//根据索引获取元素
public Object get(int index){
if(index<0 || index >= size){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
return elementData[index];
}
//删除指定位置的对象
public void remove(int index){
if(index<0 || index >= size){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
int numMoved = size - index - 1;
if(numMoved > 0){
System.arraycopy(elementData, index+1, elementData, index, numMoved);
}
elementData[--size] = null;
}
//删除某个对象元素
public void remove(Object obj){
for(int i=0; i<size; i++){
if(get(i).equals(obj)){
remove(i);
}
}
}
//替换指定位置的元素
public Object set(int index, Object obj){
Object oldValue = elementData[index];
elementData[index] = obj;
return oldValue;
}
//指定位置添加元素
public void add(int index, Object obj){
System.arraycopy(elementData, index, elementData, index+1, size-index);
elementData[index] = obj;
size ++;
}
public static void main(String[] args) {
int[] m = new int[10];
System.out.println(m.length);
int[] n = new int[20];
for(int i=0; i<m.length; i++){
n[i] =m[i];
}
m = n;
System.out.println(m.length);
}
}
2.手写LinkedList源码实现
public class SexLinkedList {
private Node first;
private Node last;
private int size;
//添加一个值
public void add(Object obj){
Node n = new Node();
if(first == null){
n.setPrevious(null);
n.setObj(obj);
n.setNext(null);
first = n;
last = n;
}else{
//直接在last后面添加节点
n.setPrevious(last);
n.setObj(obj);
n.setNext(null);
last.setNext(n);
last = n;
}
size++;
}
public int size(){
return size;
}
//返回指定索引的值
public Object get(int index){
Node temp = node(index);
if(temp!=null){
return temp.obj;
}
return null;
}
public Node node(int index){
Node temp = first;
if(first != null){
for(int i=0; i<index; i++){
temp = temp.next;
}
}
return temp;
}
//删除指定位置的指
public void remove(int index){
Node temp = node(index);
if(temp != null){
Node up = temp.previous;
Node down = temp.next;
up.next = down;
down.previous = up;
}
size--;
}
//在指定的位置添加一个值
public void add(int index, Object obj){
Node temp = node(index);
Node newNode = new Node();
newNode.obj = obj;
if(temp!= null){
Node up = temp.previous;
up.next = newNode;
newNode.previous = up;
newNode.next = temp;
temp.previous = newNode;
}
size++;
}
public static void main(String[] args) {
}
}
3.两者的比较
1.List是接口类,ArrayList和LinkedList是List的实现类。
2.ArrayList是动态数组(顺序表)的数据结构。顺序表的存储地址是连续的,所以在查找比较快,
但是在插入和删除时,由于需要把其它的元素顺序向后移动(或向前移动),所以比较熬时。
3.LinkedList是链表的数据结构。链表的存储地址是不连续的,
每个存储地址通过指针指向,在查找时需要进行通过指针遍历元素,
所以在查找时比较慢。由于链表插入时不需移动其它元素,所以在插入和删除时比较快。