package list;
public interface IList {
// 打印顺序表,注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
void display();
// 新增元素,默认在数组最后新增
void add(int data);
// 在 pos 位置新增元素
void add(int pos, int data);
// 判定是否包含某个元素
boolean contains(int toFind);
// 查找某个元素对应的位置
int indexOf(int toFind);
// 获取 pos 位置的元素
int get(int pos);
// 给 pos 位置的元素设为 value
void set(int pos, int value);
//删除第一次出现的关键字key
void remove(int toRemove);
// 获取顺序表长度
int size();
// 清空顺序表
void clear();
}
java复制代码
package list;
import java.util.Arrays;
public class MyArrayList implements IList{
public int[] array;
public int usedSize;//数组的有效的个数
//默认的容量,长度不变的
public static final int DEFAULT_SIZE = 10;
public MyArrayList() {
this.array = new int[DEFAULT_SIZE];//长度为十,默认初始化值为0
}
@Override
public void display() {
// 打印顺序表
for (int i = 0; i < this.usedSize; i++) {
System.out.print(array[i] + " ");
}
System.out.println("");
}
@Override
public void add(int data) {
// 新增元素,默认在数组最后新增
if (isFull()) {
//满了,不能放数据了,我们要进行扩容
Expansion();
}
//进行添加数据的操作
this.array[this.usedSize] = data;
this.usedSize++;
}
//对于判满这个操作,我们在别的方法中也会使用
//所以我们可以单独创建一个方法
public boolean isFull() {
//判满
return this.usedSize == DEFAULT_SIZE;
}
private void Expansion() {
//扩容操作,我们不想让别的类调用这个方法,我们只想在自己的类中调用
this.array = Arrays.copyOf(this.array,
2*this.array.length);
//我们一般按照2倍或者1.5倍进行扩容
}
private void checkPos(int pos) throws PosIsNotlegally{
if (pos < 0 || pos > usedSize) {
throw new PosIsNotlegally("Pos位置不合法");
}
}
@Override
public void add(int pos, int data) {
// 在 pos 位置新增元素
try{
checkPos(pos);//判断pos这个位置合不合法
if (isFull()) {
Expansion();
}
int end = this.usedSize;
while(pos != end) {
this.array[end] = this.array[end - 1];
end--;
}
this.array[pos] = data;
this.usedSize++;
}catch(PosIsNotlegally e) {
System.out.println("输入的Pos位置不合法");
e.printStackTrace();
}
}
@Override
public boolean contains(int toFind) {
// 判定是否包含某个元素
for (int i = 0; i < this.usedSize; i++) {
if (this.array[i] == toFind) {
return true;
}
}
return false;
}
@Override
public int indexOf(int toFind) {
// 查找某个元素对应的位置
for (int i = 0; i < this.usedSize; i++) {
if (this.array[i] == toFind) {
return i;
}
}
return -1;
}
private void checkPos2(int pos) throws PosIsNotlegally{
if (pos < 0 || pos >= usedSize) {
throw new PosIsNotlegally("Pos的位置不合法");
}
}
@Override
public int get(int pos) {
// 获取 pos 位置的元素
try {
//判断是否出现异常
checkPos2(pos);
checkEmpty();
//没有异常的话,直接返回pos下标的值
return this.array[pos];
}catch (PosIsNotlegally e) {
System.out.println("pos的位置不合法");
e.printStackTrace();
}catch (EmptyException e) {
System.out.println("顺序表为空");
e.printStackTrace();
}
return -1;
}
private boolean isEmpty() {
return usedSize == 0;
}
private void checkEmpty() throws EmptyException{
if (isEmpty()) {
throw new EmptyException("顺序表为空");
}
}
@Override
public void set(int pos, int value) {
// 给 pos 位置的元素设为 value
try {
//这个也要判断异常
checkPos2(pos);
checkEmpty();
//没有异常给pos位置赋值
this.array[pos] = value;
}catch (PosIsNotlegally e) {
System.out.println("pos位置不合法");
e.printStackTrace();
}catch (EmptyException e) {
System.out.println("顺序表为空");
e.printStackTrace();
}
}
@Override
public void remove(int toRemove) {
//删除第一次出现的关键字toRemove
try{
checkEmpty();
int pos = indexOf(toRemove);
if (pos == -1) {
return;
}
for (int i = pos; i < usedSize - 1; i++) {
array[i] = array[i + 1];
}
usedSize--;
}catch (EmptyException e) {
System.out.println("顺序表为空");
e.printStackTrace();
}
}
@Override
public int size() {
// 获取顺序表长度
return this.usedSize;
}
@Override
public void clear() {
// 清空顺序表
// for (int i = 0; i < usedSize; i++) {
// array[i] = null;
// }
usedSize = 0;
}
}
java复制代码
package list;
public class PosIsNotlegally extends RuntimeException{
public PosIsNotlegally() {
}
public PosIsNotlegally(String message) {
super(message);
}
}
java复制代码
package list;
public class EmptyException extends RuntimeException{
public EmptyException() {
}
public EmptyException(String message) {
super(message);
}
}