import java.util.Arrays;
public class MyArraylist {
public int[] elem;
public int usedSize;//0
//默认容量
private static final int DEFAULT_SIZE = 10;
public MyArraylist() {
this.elem = new int[DEFAULT_SIZE];
}
/**
* 打印顺序表:
* 根据usedSize判断即可
*/
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()) {
System.out.println("原数组已经满了");
this.elem =
Arrays.copyOf(this.elem, 2 * this.elem.length);
}
this.elem[this.usedSize] = data;
this.usedSize++;
}
/**
* 判断当前的顺序表是不是满的!
*
* @return true:满 false代表空
*/
public boolean isFull() {
return this.usedSize >= this.elem.length;
}
private boolean checkPosInAdd(int pos) {
if (pos >= this.elem.length || pos < 0) {
System.out.println("所输入的pos不合法");
return false;
}
return true;//合法
}
// 在 pos 位置新增元素
public void add(int pos, int data) throws PosWrongfulException {
if (isFull()) {
System.out.println("原数组已满");
this.elem =
Arrays.copyOf(this.elem, 2 * this.elem.length);
}
if (!checkPosInAdd(pos)) {
System.out.println("所输入的pos不合法");
throw new PosWrongfulException("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] == this.elem[toFind]) {
return true;
}
}
System.out.println("未包含该元素");
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_pos ( int pos){
if(isEmpty()){
System.out.println("数组为空无法获取");
throw new EmptyException("当前顺序表为空!");
}
if(!checkPosInAdd(pos)){
System.out.println("所输入的pos不合法");
throw new PosWrongfulException("pos位置不合法");
}
return this.elem[pos];
}
private boolean isEmpty () {
return this.usedSize == 0;
}
// 给 pos 位置的元素设为【更新为】 value
public void set_pos ( int pos, int value){
if(isEmpty()){
System.out.println("数组为空无法获取");
throw new EmptyException("当前顺序表为空!");
}
if(!checkPosInAdd(pos)){
System.out.println("所输入的pos不合法");
throw new PosWrongfulException("pos位置不合法");
}
this.elem[pos]=value;
}
/**
* 删除第一次出现的关键字key
* @param key
*/
public void remove ( int key){
if(isEmpty()){
System.out.println("数组为空无法获取");
throw new EmptyException("当前顺序表为空!");
}
if(!checkPosInAdd(key)){
System.out.println("所输入的key不合法");
throw new PosWrongfulException("key位置不合法");
}
int index=indexOf(key);
if(index==-1){
System.out.println("不存在此数字");
}
for(int i=index;i<this.usedSize;i++){
this.elem[i]=this.elem[i+1];
}
}
// 获取顺序表长度
public int size () {
return this.usedSize;
}
// 清空顺序表
public void clear () {
for(int i=0;i<this.usedSize;i++){
this.elem[i]=0;
this.usedSize--;
}
}
}
----------------------分割线-------------------------------
public class PosWrongfulException extends RuntimeException{
public PosWrongfulException() {
}
public PosWrongfulException(String message) {
super(message);
}
}
---------------------分割线---------------------------------
public class EmptyException extends RuntimeException{
public EmptyException() {
}
public EmptyException(String message) {
super(message);
}
}
简单实现Arraylist链表底层代码
最新推荐文章于 2024-09-07 17:58:28 发布