目录
ArrayList类的实现
在Java数据结构中,ArrayList是通过数组实现,通过索引(数组下标)来访问元素,支持快速随机访问。
import java.util.Arrays;
public class MyArrayList {
private int [] elem;
//原来存放数据元素
private int usedSize;
// 代表当前顺序表当中的有效数据个数
public MyArrayList() {
this.elem = new int[1000];
}
/*
* 指定容量,实际上是变量数组
*/
public MyArrayList(int initCapacity) {
this.elem = new int [initCapacity];
}
}
判断是否ArrayList为满
//判断是否为满
public boolean isFull() {
if(this.usedSize == this.elem.length) {
return true;
}
return false;
}
在数组结尾增加元素
//新增元素,在数组最后加
public void add(int data) {
if(isFull()) {
//扩容
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
this.elem[this.usedSize] = data;
usedSize++;
}
判断是否包含每个元素
// 判断是否包含某个元素
public boolean contains(int toFind) {
for(int i = 0;i < this.usedSize;i++) {
if(this.elem[i] == toFind) {
return true;
}
}
return false;
}
查找某个元素对应位置,查找某个位置对应元素
//查找某个元素对应位置
public int indexOf(int toFind) {
for(int i = 0; i <=this.usedSize;i++) {
if(this.elem[i] == toFind) {
return i;
}
}
return -1;
}
//获取pos 位置的元素
public int get(int pos) {
if(pos < 0 || pos > this.usedSize) {
System.out.println("位置不合法");
}
return this.elem[pos];
}
获取顺序表长度,清空顺序表
//获取顺序表长度
public int size() {
return this.usedSize;
}
//清空顺序表
public void clear() {
this.usedSize = 0;
}
实现ArrayList最难的点在于增加和删除元素
ArrayList的增加
//在pos位置新增元素
public void add(int pos,int data) {
if(pos < 0 || pos > this.usedSize) {
System.out.println("位置不合法");
}
if(isFull()) {
//扩容
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
//数组中间加入新的元素,要保持原有数据不被替换,须在把数组 pos位置给空出来
//采用将pos(包括pos)后面的元素,都往后移动一个单位
for(int i = this.usedSize-1;i >= pos;i++ ) {
this.elem[i+1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
ArrayList的删除
删除的逻辑和增加类似
//删除第一次出现的关键字
public void remove(int toRemove) {
int index = indexOf(toRemove);
if(index == -1) {
System.out.println("没有这个数据!");
}
//这里的逻辑和 在pos位置增加一个元素类似
// 当数组少一个元素时,后面的元素须得都往前一个位置,把这个被删除的位置给补上
for(int i = index;i <= this.usedSize;i++) {
this.elem[i] = this.elem[i+1];
}
//this.elem[usedSize-1] = null;
this.usedSize--;
}
全部代码展示
import java.util.Arrays;
public class MyArrayList {
private int [] elem;
//原来存放数据元素
private int usedSize;
// 代表当前顺序表当中的有效数据个数
public MyArrayList() {
this.elem = new int[1000];
}
/*
* 指定容量,实际上是变量数组
*/
public MyArrayList(int initCapacity) {
this.elem = new int [initCapacity];
}
public void display() {
for(int i = 0;i < this.usedSize;i++) {
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
//新增元素,默认在数组最后加
public void add(int data) {
if(isFull()) {
//扩容
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
this.elem[this.usedSize] = data;
usedSize++;
}
//判断是否为满
public boolean isFull() {
if(this.usedSize == this.elem.length) {
return true;
}
return false;
}
//在pos位置新增元素
public void add(int pos,int data) {
if(pos < 0 || pos > this.usedSize) {
System.out.println("位置不合法");
}
if(isFull()) {
//扩容
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
//数组中间加入新的元素,要保持原有数据不被替换,须在把数组 pos位置给空出来
//采用将pos(包括pos)后面的元素,都往后移动一个单位
for(int i = this.usedSize-1;i >= pos;i++ ) {
this.elem[i+1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
// 判断是否包含每个元素
public boolean contains(int toFind) {
for(int i = 0;i < this.usedSize;i++) {
if(this.elem[i] == toFind) {
return true;
}
}
return false;
}
//查找某个元素对应位置
public int indexOf(int toFind) {
for(int i = 0; i <=this.usedSize;i++) {
if(this.elem[i] == toFind) {
return i;
}
}
return -1;
}
//获取pos 位置的元素
public int get(int pos) {
if(pos < 0 || pos > this.usedSize) {
System.out.println("位置不合法");
}
return this.elem[pos];
}
//删除第一次出现的关键字
public void remove(int toRemove) {
int index = indexOf(toRemove);
if(index == -1) {
System.out.println("没有这个数据!");
}
//这里的逻辑和 在pos位置增加一个元素类似
// 当数组少一个元素时,后面的元素须得都往前一个位置,把这个被删除的位置给补上
for(int i = index;i <= this.usedSize;i++) {
this.elem[i] = this.elem[i+1];
}
//this.elem[usedSize-1] = null;
this.usedSize--;
}
//获取顺序表长度
public int size() {
return this.usedSize;
}
//清空顺序表
public void clear() {
this.usedSize = 0;
}
}