今日份鼓励:努力的人生才是最好的,不要在黄金一般的年纪荒废了青春,真正能激励我们前行是身边朋友的积极上进是与充满正能量的人同行!!!
目录
知识总结:
1.线性表不仅在物理上存储空间连续,并且在逻辑上也连续
2.线性表适合查找,搜索,但是在插入与删除当中非常繁琐,并且存在空间的浪费
3.顺序表可以说就是数组
一、线性表中的顺序表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串..线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的。
顺序表在物理上存储是连续的并且在逻辑上也是连续的,通常以数组的形式存储
二、顺序表的实现刨析
1.打印顺序表
分析:
1.定义一个变量来确定顺序表的有效长度,不能使用数组名.length方法
2.使用this.的习惯,避免出错
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
2.判断顺序表是否填满
分析:
1.直接让数组的有效长度与数组大小比较,相等即使数组元素填满了
2.我们也可以将它补充为一个扩容方法,一半顺序表的扩容都是原来大小的2倍
public boolean is_Full(){
return this.usedSize == this.elem.length;
}
3.在 pos 位置新增元素
分析:
1.我们需要先判断输入的下标位置是否正确,正确的下标不应该小于0,并且不能大于数组的有效长度
2.在判断下标位置正确后,我们同样需要判断数组的能否再存储下一个元素,所以我们需要在插入元素之前判断数组满了没有,来确定是否扩容
3.在最后将元素添加后,我们需要将数组的有效长度加一
public void add(int pos, int data) {
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合适!!");
return ;
}
if(is_Full()){
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
for (int i = usedSize-1; i >= pos ; i--) {
this.elem[i+1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
4.判定是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i < this.usedSize-1; i++) {
if(toFind == this.elem[i]){
return true;
}
}
return false;
}
5.查找某个元素对应的位置
public int search(int toFind) {
for (int i = 0; i < this.usedSize-1; i++) {
if(toFind == this.elem[i]){
return i;
}
}
return -1;
}
6.获取 pos 位置的元素
public int getPos(int pos) {
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合适!!");
return -1;
}
for (int i = 0; i < this.usedSize-1; i++) {
if(i == pos){
return this.elem[pos];
}
}
return -1;
}
7.给 pos 位置的元素设为 value
public void setPos(int pos, int value) {
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合适!!");
}
for (int i = 0; i < this.usedSize-1; i++) {
if(i == pos){
this.elem[pos] = value;
}
}
}
8.删除第一次出现的关键字key
public void remove(int toRemove) {
int indes = search(toRemove);
if(indes == -1){
System.out.println("没有你要删除的元素");
}
for (int i = indes; i < this.usedSize-1; i++) {
this.elem[i] = this.elem[i+1];
}
//this.elem[usedSize] == null;
this.usedSize--;
}
9.清空顺序表
法一:不推荐使用
原因:如果平常情况下我们直接制空不会影响JVM的垃圾回收机制
但是如果我们的元素是一群引用,那么我们的元素的引用指向的对象依旧存在指向它的引用,那么就会造成内存泄露
法二:将每一个顺序表的元素都制空
public void clear() {
this.usedSize =0;
/* for (int i = 0; i < usedSize; i++) {
this.elem[i] = null
}
this.usedSize = 0;
*/
}
三、JAVA的实现源码源码
①MyArrayList.java文件
package com.company;
import java.lang.reflect.Array;
import java.util.Arrays;
/**
* JAVA实现顺序表
*/
public class MyArrayList {
public int[] elem; //顺序表元素
public int usedSize;//顺序表实际大小
public MyArrayList() {
this.elem = new int[10];
}
// 打印顺序表
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i]+" ");
}
System.out.println();
}
//判断顺序表是否填满
public boolean is_Full(){
return this.usedSize == this.elem.length;
}
// 在 pos 位置新增元素
public void add(int pos, int data) {
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合适!!");
return ;
}
if(is_Full()){
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
for (int i = 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-1; i++) {
if(toFind == this.elem[i]){
return true;
}
}
return false;
}
// 查找某个元素对应的位置
public int search(int toFind) {
for (int i = 0; i < this.usedSize-1; i++) {
if(toFind == this.elem[i]){
return i;
}
}
return -1;
}
// 获取 pos 位置的元素
public int getPos(int pos) {
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合适!!");
return -1;
}
for (int i = 0; i < this.usedSize-1; i++) {
if(i == pos){
return this.elem[pos];
}
}
return -1;
}
// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) {
if(pos < 0 || pos > this.usedSize){
System.out.println("pos位置不合适!!");
}
for (int i = 0; i < this.usedSize-1; i++) {
if(i == pos){
this.elem[pos] = value;
}
}
}
//删除第一次出现的关键字key
public void remove(int toRemove) {
int indes = search(toRemove);
if(indes == -1){
System.out.println("没有你要删除的元素");
}
for (int i = indes; i < this.usedSize-1; i++) {
this.elem[i] = this.elem[i+1];
}
//this.elem[usedSize] == null;
this.usedSize--;
}
// 获取顺序表长度
public int size() {
return this.usedSize;
}
// 清空顺序表
public void clear() {
this.usedSize =0;
/* for (int i = 0; i < usedSize; i++) {
this.elem[i] = null
}
this.usedSize = 0;
*/
}
}
②测试文件Test.java
package com.company;
public class Test {
//public static int a =10;
public static void main(String[] args) {
// write your code here
MyArrayList myArrayList = new MyArrayList();
myArrayList.display();
//在零位置增加元素
myArrayList.add(0,1);
myArrayList.add(0,2);
myArrayList.add(0,3);
myArrayList.add(0,4);
myArrayList.add(0,5);
myArrayList.add(0,6);
myArrayList.add(0,7);
myArrayList.display();
boolean falg = myArrayList.contains(5);
System.out.println(falg);
int ret = myArrayList.search(5);
System.out.println(ret);
int ret2 =myArrayList.getPos(2);
System.out.println(ret2);
myArrayList.setPos(2,10);
myArrayList.display();
myArrayList.remove(10);
myArrayList.display();
}
}