👀作者简介:大家好,我是大杉。
🚩🚩 个人主页:爱编程的大杉
支持我:点赞+关注~不迷路🧡🧡🧡
✔系列专栏:javase基础⚡⚡⚡
(❁´◡`❁)励志格言:真实的自我就像月光下的海,庞大,安静,想证明给别人看的时候,却只能拍出一团焦糊的黑暗。(from 《时间的女儿》)
🤞🤞
文章目录
一.顺序表成员变量的组成解析
1.顺序表本质就是一个数组,所以要定义一个数组(int [] elem)来存放顺序表的元素
2.在为顺序表增添元素时,根据需求不一定需要将顺序表填满,所以需要一个变量(usedsize)来记录顺序表的有效数据个数,以便其他功能的实现
//成员变量定义及初始化
public int usedsize;
public int[] elem;
public static final int CAPACITY = 10;
//数组容量
public MyList() {
this.usedsize = 0;
this.elem = new int[CAPACITY];
}
二 .在指定位置新增元素
private boolean isFull()
{
return this.usedSize==this.elem.length;
}
public void add(int pos, int data) {
//判断顺序表是否越界
if(pos<0||pos>usedSize)
{
throw new RuntimeException("pos不合法");
}
//判断顺序表是否已经占满
if(isFull())
{
//如果已经满了,将数组扩容2倍
this.elem= Arrays.copyOf(this.elem,2*this.elem.length);
}
//后移一位,腾出以为空间
for (int i = usedSize - 1; i >= pos; i--) {
elem[i + 1] = elem[i];
}
this.elem[pos] = data;
usedSize++;
}
二、判断是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i <this.usedSize; i++) {
if(elem[i]==toFind)
{
return true;
}
}
return false;
}
三.查找某个元素对应的位置
// 查找某个元素对应的位置
public int search(int toFind) {
for (int i = 0; i <this.usedSize ; i++) {
if(elem[i]==toFind)
return i;
}
return -1;
}
四.获取特定位置的元素
//封装一个方法判断顺序表该位置是否为空
private boolean isEmpty()
{
return this.usedSize==0;
}
// 获取 pos 位置的元素
public int getPos(int pos) {
if(isEmpty())
{
//手动抛出异常
throw new RuntimeException("顺序表已经满了");
}
if(pos<0||pos>=usedSize)
{
throw new RuntimeException("pos不合法");
}
return this.elem[pos];
}
五. 打印顺序表
public void display() {
for (int i = 0; i <this.usedSize ; i++) {
System.out.print(elem[i]+" ");
}
System.out.println();
}
六.将指定位置的元素设为 value
//检测合法性
public void setPos(int pos, int value) {
if(pos<0||pos>=usedSize)
//要加上“=”因为如果pos=usedSize则pos位置为空无法更改
{
throw new RuntimeException("pos不合法");
}
this.elem[pos]=value;
}
七.删除特定元素
删除特定元素的原理是让后面的元素前移一位,覆盖要删除的元素
//封装一个方法获得所要删除元素的下标
private int getIndex(int toRemove)
{
int i=0;
for (i = 0; i <this.usedSize ; i++) {
if(this.elem[i]==toRemove)
{
break;
}
}
return i;
}
//删除第一次出现的关键字key
public void remove(int toRemove) {
int j=0;
j=getIndex(toRemove);
for (int i = j; i <usedSize ; i++) {
//前移一位
this.elem[i]=this.elem[i+1];
}
//有效元素数减一
usedSize--;
}
八.获取顺序表长度
// 获取顺序表长度
public int size() {
return this.usedSize;
}
九.清空顺序表
public void clear() {
System.out.println("clear()");
this.usedSize=0;
}
总体整合
import java.util.Arrays;
class MyList {
public int[] elem;
public int usedSize;
public static final int capacity = 10;
public MyList() {
this.elem = new int[capacity];
this.usedSize = 0;
}
// 打印顺序表
public void display() {
for (int i = 0; i <this.usedSize ; i++) {
System.out.print(elem[i]+" ");
}
System.out.println();
}
//判断pos位置是否为空
private boolean isFull()
{
return this.usedSize==this.elem.length;
}
// 在 pos 位置新增元素
public void add(int pos, int data) {
if(pos<0||pos>usedSize)
{
throw new RuntimeException("pos不合法");
}
if(isFull())
{
this.elem= Arrays.copyOf(this.elem,2*this.elem.length);
}
for (int i = usedSize - 1; i >= pos; i--) {
elem[i + 1] = elem[i];
}
this.elem[pos] = data;
usedSize++;
}
// 判定是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i <this.usedSize; i++) {
if(elem[i]==toFind)
{
return true;
}
}
return false;
}
// 查找某个元素对应的位置
public int search(int toFind) {
for (int i = 0; i <this.usedSize ; i++) {
if(elem[i]==toFind)
return i;
}
return -1;
}
private boolean isEmpty()
{
return this.usedSize==0;
}
// 获取 pos 位置的元素
public int getPos(int pos) {
if(isEmpty())
{
throw new RuntimeException("顺序表已经满了");
}
if(pos<0||pos>=usedSize)
{
throw new RuntimeException("pos不合法");
}
return this.elem[pos];
}
// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) {
if(pos<0||pos>=usedSize)
//要加上“=”因为如果pos=usedSize则pos位置为空无法更改
{
throw new RuntimeException("pos不合法");
}
this.elem[pos]=value;
}
private int getIndex(int toRemove)
{
int i=0;
for (i = 0; i <this.usedSize ; i++) {
if(this.elem[i]==toRemove)
{
break;
}
}
return i;
}
//删除第一次出现的关键字key
public void remove(int toRemove) {
int j=0;
j=getIndex(toRemove);
for (int i = j; i <usedSize ; i++) {
this.elem[i]=this.elem[i+1];
}
usedSize--;
}
// 获取顺序表长度
public int size() {
return this.usedSize;
}
// 清空顺序表
public void clear() {
System.out.println("clear()");
this.usedSize=0;
}
}
public class MyArrayList {
public static void main(String[] args) {
MyList my=new MyList();
//为顺序表添加元素
for (int i = 0; i <10 ; i++) {
my.add(i,i);
}
my.display();
//为10号下标赋值检测是否扩容
my.add(10,120);
my.display();
System.out.println("===========");
System.out.println(my.contains(9));
System.out.println(my.search(9));
System.out.println("===========");
System.out.println(my.getPos(9));
System.out.println("===========");
//测试:删除首尾和中间
my.remove(2);
my.remove(0);
my.remove(10);
my.display();
System.out.println("==========");
my.setPos(2,1000);
my.display();
System.out.println("==========");
my.clear();
my.display();
}
}