您的位置:首页 > 理论基础 > 数据结构算法

java数据结构之(一):ArrayList类的实现

2017-07-28 20:53 423 查看
  ArrayList是链表(List)的一种实现,通过数组的方式实现的,所具有的特点就是数组本身的特点,比如取数据较快,删除和添加元素比较耗时等等,后续日志需要详细介绍ArrayList的特点及原理。先贴上ArrayList的实现代码(数据结构与算法分析_java版)。

package com.biyao.datastructure.list;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* @ClassName: MyArrayList
* @Description: ArrayList实现
* @author yangy
* @date 2017年7月28日 下午3:01:47
*/
public class MyArrayList<AnyType>{

//默认容量
private static final int DEFAULT_CAPACITY = 10;
//list的长度
private int theSize;
//元素数组
private AnyType [] theItems;
/**
* <p>Title: 构造方法</p>
* <p>Description: 需清空链表</p>
*/
public MyArrayList(){
clear();
}

/**
* @Title: clear
* @Description: 清空链表
* @return void
*/
public void clear(){
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
/**
* @Description: 获取链表大小
* @return int 链表大小
*/
public int size(){
return theSize;
}

/**
* @Description: 判断链表是否为空
* @return
*/
public boolean isEmpty(){
return size() == 0;
}

public void trimToSize(){
ensureCapacity(size());
}

/**
* @Description: 根据索引获取链表元素
* @param idx 索引值
* @return 链表中索引值对应的元素值
*/
public AnyType get(int idx){
if(idx < 0 || idx >= size()){
throw new ArrayIndexOutOfBoundsException();
}
return theItems[idx];
}

/**
* @Description: 向链表中指定索引处添加元素
* @param idx 索引值
* @param newVal 添加的元素
* @return 链表set前索引处的旧元素
*/
public AnyType set(int idx,AnyType newVal){
if(idx < 0 || idx >= size()){
throw new ArrayIndexOutOfBoundsException();
}
AnyType old = theItems[idx];
theItems[idx] = newVal;
return old;
}

/**
* @Description: 链表扩容
* @param newCapacity 链表新容量大小
* @return void
*/
@SuppressWarnings("unchecked")
public void ensureCapacity(int newCapacity){
if(newCapacity <= theSize){
return ;
}
AnyType [] old = theItems;
theItems = (AnyType[])new Object[newCapacity];
for (int i = 0; i < theSize; i++) {
theItems[i] = old[i];
}

}

/**
* @Description: 向链表中添加元素
* @param x 新元素
* @return 添加是否成功
*/
public boolean add(AnyType x){
add(size(),x);
return true;
}

/**
* @Description: 向链表固定索引位置添加元素
* @param idx 索引位置
* @param x 待添加元素
*/
public void add(int idx,AnyType x){
if(theItems.length == size()){
ensureCapacity(size()*2 + 1);
}
for (int i = theSize; i > idx; i--) {
theItems[i] = theItems[i-1];
}
theItems[idx] = x;
theSize++;
}

/**
* @Description: 删除链表指定索引位置的元素
* @param idx 索引位置
* @return 被删元素
*/
public AnyType remove(int idx){
AnyType removeItem = theItems[idx];
for (int i = idx; i < size()-1; i++) {
theItems[i] = theItems[i+1];
}
theSize--;
return removeItem;
}

public Iterator<AnyType> iterator(){
return new ArrayListIterator();
}

private class ArrayListIterator implements Iterator<AnyType>{

private int current = 0;

@Override
public boolean hasNext() {
return current < size();
}

@Override
public AnyType next() {
if(!hasNext()){
throw new NoSuchElementException();
}
return theItems[current++];
}

public void remove(){
MyArrayList.this.remove(--current);
}
}

}


测试代码如下:

package com.biyao.datastructure.list;

import java.util.Iterator;

/**
* @ClassName: MyArrayListExample
* @Description: MyArrayList 测试类
* @author yangy
* @date 2017年7月28日 下午7:21:49
*/
public class MyArrayListExample {

public static void main(String[] args) {
MyArrayList<String> arrayList = new MyArrayList<String>();
System.out.println("arrayList是否为空1:" + arrayList.isEmpty());
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
System.out.println("arrayList是否为空2:" + arrayList.isEmpty());
System.out.println("arrayList大小为:" + arrayList.size());
System.out.println("arrayList.get(1)值为:" + arrayList.get(1));
System.out.println("arrayList.remove(1)返回值为:" + arrayList.remove(1));
System.out.println("arrayList.get(1)值为:" + arrayList.get(1));
arrayList.set(1, "d");
System.out.println("arrayList.get(1)值为:" + arrayList.get(1));

//遍历list
Iterator<String> itor = arrayList.iterator();
System.out.println("遍历list:");
while(itor.hasNext()){
String s = itor.next();
if(s.equals("d")){
itor.remove();
}
System.out.print(s + " ");
}
System.out.println();
System.out.println("arrayList大小为:" + arrayList.size());
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息