《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
}
}
3.在pos位置插入元素
public void add(int pos,int data){
//检验 pos的合法性
if(pos < 0 || pos > this.usedSize){
return;
}
for (int i = this.usedSize-1; i >= pos ; i–) {
this.array[i+1] = this.array[i];
}
this.array[pos] = data;
this.usedSize++;
}
但上述代码有个问题,若数组内已经存满元素,便不能再直接插入元素,需要扩容
则改进后:
private boolean isFull(){
return this.usedSize==this.array.length;
}
//2.在pos位置插入data
public void add(int pos,int data){
//判断数组是否存满
if(isFull()){
//二倍扩容
this.array = Arrays.copyOf(this.array,2*this.array.length);
}
//检验pos的合法性
if(pos < 0 || pos > this.usedSize){
return;
}
for (int i = this.usedSize-1; i >= pos ; i–) {
this.array[i+1] = this.array[i];
}
this.array[pos] = data;
this.usedSize++;
}
注意:
-
先要判断pos的合法性,负数、大于usedSize均不合法
-
检验数组是否需要扩容
-
挪数据,i 从usedSize-1 开始,[i+1]=[i]
-
插入完成后,this.array[pos]=data
4.判断是否包含某个元素
public boolean contain(int toFind){
for (int i = 0; i < this.usedSize; i++) {
if(this.array[i] == toFind){
return true;
}
}
return false;
}
5.查找某个元素对应的位置
public int search(int toFind){
for (int i = 0; i < this.usedSize; i++) {
if(this.array[i] == toFind){
return i;
}
}
return -1;
}
6.获取pos位置的元素
public int getPos(int pos){
//①检验顺序表是否为空
if(isEmpty()){
throw new RuntimeException(“顺序表为空!”); //手动抛出异常
}
//②检验pos合法性
if(pos < 0 || pos >= usedSize){
throw new RuntimeException(“pos不合法!”);
}
return this.array[pos];
}
7.给pos位置的元素设为value
public void setPos(int pos,int value){
this.array[pos] = value;
}
8.删除第一次出现的关键字key
public void delete(int key){
int tmp = search(key);
if(tmp == -1){
System.out.println(“没有需要删除的数字!”);
return;
}
for (int i = tmp; i < this.usedSize-1; i++) {
this.array[i] = this.array[i+1];
}
this.usedSize–;
}
9.获取顺序表长度
public int len(){
return this.usedSize;
}
10.清空顺序表
public void clear(){
this.usedSize = 0;
}
以上是顺序表的所有操作
附全部代码:
顺序表:
import java.util.Arrays;
//1.新建一个顺序表
public class MyArrayList {
public int[] array; //数组
public int usedSize; //有效的数据个数
public static final int intCapacity = 10; //初始容量
public MyArrayList(){
this.array = new int[intCapacity];
this.usedSize = 0;
}
//2.打印顺序表
public void print(){
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.array[i]+" ");
}
System.out.println();
}
//判断数组是否存储满
private boolean isFull(){
return this.usedSize == this.array.length;
}
//3.在pos位置插入data
public void add(int pos,int data){
//判断数组是否存满
if(isFull()){
//二倍扩容
this.array = Arrays.copyOf(this.array,2*this.array.length);
}
//检验pos的合法性
checkPos(pos);
// if(pos < 0 || pos > this.usedSize){
// return;
// }
for (int i = this.usedSize-1; i >= pos ; i–) {
this.array[i+1] = this.array[i];
}
this.array[pos] = data;
this.usedSize++;
}
//4.判断是否包含某个元素
public boolean contain(int toFind){
for (int i = 0; i < this.usedSize; i++) {
if(this.array[i] == toFind){
return true;
}
}
return false;
}
//5.查找某个元素对应的位置
public int search(int toFind){
for (int i = 0; i < this.usedSize; i++) {
if(this.array[i] == toFind){
return i;
}
}
return -1;
}
//检验合法性 封装为方法
private void checkPos(int pos){
if(pos < 0 || pos > usedSize){
throw new RuntimeException(“pos不合法!”);
}
}
//判断顺序表是否为空
private boolean isEmpty(){
return this.usedSize == 0;
}
//6.获取pos位置的元素
public int getPos(int pos){
//①检验顺序表是否为空
if(isEmpty()){
//return -1;
throw new RuntimeException(“顺序表为空!”); //手动抛出异常
}
//②检验pos合法性
if(pos < 0 || pos >= usedSize){
最后
每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。
如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份限量版的Java零基础宝典能够对你有所帮助。
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
表是否为空
if(isEmpty()){
//return -1;
throw new RuntimeException(“顺序表为空!”); //手动抛出异常
}
//②检验pos合法性
if(pos < 0 || pos >= usedSize){
最后
每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。
如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份限量版的Java零基础宝典能够对你有所帮助。
[外链图片转存中…(img-liNyb44k-1714453655011)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!