要实现以下功能:
// 打印顺序表
public void display() { }
// 在 pos 位置新增元素
public void add(int pos, int data) { }
// 判定是否包含某个元素
public boolean contains(int toFind) { return true; }
// 查找某个元素对应的位置
public int search(int toFind) { return -1; }
// 获取 pos 位置的元素
public int getPos(int pos) { return -1; }
// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) { }
//删除第一次出现的关键字key
public void remove(int toRemove) { }
// 获取顺序表长度
public int size() { return 0; }
// 清空顺序表
public void clear() { }
MyArrayList是放类和各种方法的。
TestDemo是放main函数,调用MyArrayList的程序的。
MyArrayList:
import java.util.Arrays;
public class MyArrayList {
public int[] elem;//数组,此处想放什么类型都可以改,例如double,Person类型也可以,
// 只是在开头要class Person()一下
public int usedSize;//有效的数据个数
public static final int capacity = 10;//初始容量
public MyArrayList() {
this.elem = new int[capacity];
this.usedSize = 0;
}
// 打印顺序表
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
// 在 pos 位置新增元素
private boolean isFull(){
if(this.usedSize == this.elem.length){
return true;
}
return false;
}
private void checkPos(int pos){
if(pos < 0 || pos >= usedSize){
throw new RuntimeException("pos位置不合法!");
}
}
public void add(int pos, int data) {
checkPos(pos);//检查pos位置是否合法,只要抛出异常,代码就停到这行不走了
if(isFull()){
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
/*if(pos < 0 || pos > usedSize){//检查是否合法的最原始的另一种方法
return;
}*/
for(int i =usedSize - 1 ;i >= pos; i--){
this.elem[i + 1] =this.elem[i];
}
this.elem[pos]=data;
this.usedSize ++;//在TestDemo里面有一个添加数组元素的过程,由此usedSize++,才有了数值
}
// 判定是否包含某个元素
public boolean contains(int toFind) {
for(int i = 0; i < this.usedSize; i++){
if(toFind == this.elem[i]){
return true;
}
}
return false;
}
// 查找某个元素对应的位置
public int search(int toFind) {
for(int i = 0; i < this.usedSize; i++){
if(toFind == this.elem[i]){
return i;
}
}
return -1;
}
//获取 pos 位置的元素
private boolean isEmpty(){
return this.usedSize == 0;//不能写null,因为null是没分配内存,写0是分配内存,没有元素
}
public int getPos(int pos) {
if(isEmpty()){ //1.顺序表是否为空
//return -1;
throw new RuntimeException("顺序表为空!");//手动抛出异常
}
/*if(pos < 0 || pos >= usedSize){ //2.合法性(负数,>usedSize)
return -1;
}*/
checkPos(pos);//合法性
return this.elem[pos];
}
// 获取顺序表长度
public int size() {
return this.usedSize;
}
// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) {
checkPos(pos);
this.elem[pos]= value;
}
// 删除第一次出现的关键字key
public void remove(int toRemove) {
/*int index = 0;
for(int i = 0; i < this.usedSize - 1; i ++){
if(toRemove == this.elem[i]){
index =i;
}
}*/
int index = search(toRemove);
if(index == -1){
System.out.println("没有需要删除的数字!");
return;
//此处必要return,不然index=-1,还会继续往下执行!
}
for(int i = index; i <this.usedSize - 1; i++){
this.elem[i] = this.elem[i+1];
}
this.usedSize--;
}
// 清空顺序表
public void clear() {
this.usedSize = 0;//可用的有效数字为0
}
}
TestDemo:
public class TestDemo {
public static void main(String[] args) {
MyArrayList myArrayList1 = new MyArrayList();
for(int i = 0; i < 10; i ++){
myArrayList1.add(i,i);
}
myArrayList1.display();
myArrayList1.add(2,12);
myArrayList1.add(10,20);
//保证一个一个去放,才能扩容,不要跳着去放
myArrayList1.display();
System.out.println("=====================");
System.out.println(myArrayList1.contains(51));
myArrayList1.contains(5);
System.out.println(myArrayList1.search(21));
System.out.println("=====================");
System.out.println(myArrayList1.size());
System.out.println(myArrayList1.getPos(14));
myArrayList1.remove(9);
myArrayList1.clear();
myArrayList1.display();
myArrayList1.setPos(3,25);
myArrayList1.display();
}
}