**
顺序存储定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构
顺序表的特点:以物理位置相邻表达逻辑关系
顺序表的优点:任一元素可随机存取
顺序表的缺点:进行插入和删除操作时,需要移动大量元素
**
依次存取,地址连续——中间没有空余的存储结构
package SequentialList;
import java.util.Arrays;
public class IList {
public int[] elem; //存储空间的基地址
public int usedSize; //当前已被使用的空间大小
private static final int DEFAULT_SIZE = 10; //默认容量
public IList() {
this.elem = new int[DEFAULT_SIZE];
}
//顺序表的打印
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
//获取顺序表的长度
public int size() {
return this.usedSize;
}
//判断数组容量是否充足
public boolean isAdequate() {
if (this.elem.length >= this.usedSize) {
return true;
}else{
return false;
}
}
/*
默认在数组最后位置新增的
数组容量进行二倍扩容
*/
public void addTail(int data){
if (!this.isAdequate()){
this.elem = Arrays.copyOf(this.elem, this.usedSize * 2);
}
this.elem[this.usedSize] = data;
this.usedSize++;
}
/*
在数组的指定位置新增
*/
public void addSpecified(int data, int position) {
if (!this.isAdequate()){
this.elem = Arrays.copyOf(this.elem, this.usedSize * 2);
}
if (position < 0 || position > this.usedSize) {
System.out.println("position参数不合法");
}else{
for (int i = this.usedSize - 1; i >= position; i--) {
this.elem[i + 1] = this.elem[i];
}
this.elem[position] = data;
this.usedSize++;
}
}
/*
查找是否包含某个元素
*/
public boolean toFind(int digital) {
for (int i = 0; i < this.elem.length; i++) {
if (elem[i]==digital) {
return true;
}
}
return false;
}
/*
查找某个元素的index
*/
public int indexOf(int digital) {
for (int i = 0; i < this.elem.length; i++) {
if (elem[i]==digital) {
return i;
}
}
return -1;
}
private boolean isEmpty() {
if (this.usedSize == 0) {
return true;
}
return false;
}
/*
获取position的位置
*/
public int get(int position) {
if (this.isEmpty()) {
System.out.println("顺序表里没有元素"); //感觉抛异常好一点
}
if (position < 0 || position > this.usedSize) {
System.out.println("position参数不合法");
}
return this.elem[this.indexOf(position)];
}
/*
修改指定位置的值
*/
public void setValue(int position, int value) {
if (this.isEmpty()){
System.out.println("顺序表里没有元素"); //感觉抛异常好一点
}
if (position < 0 || position > this.usedSize) {
System.out.println("position参数不合法");
}
this.elem[position] = value;
}
/*
删除指定元素
*/
public void removeDigital(int value) {
if (this.isEmpty()){
System.out.println("顺序表里没有元素");
}
int key = this.indexOf(value);
if (key==-1){
System.out.println("顺序表里没有该元素");
}else{
for (int i = key+1; i < this.usedSize; i++) {
this.elem[i-1] = this.elem[i];
}
this.usedSize--;
}
}
/*
删除指定pos的value
*/
public void removePos(int pos) {
if (!this.isEmpty()){
if (pos < 0 || pos > this.usedSize){
System.out.println("输入参数非法");
}else{
for (int i = pos-1; i < this.usedSize; i++) {
this.elem[i] = this.elem[i+1];
}
this.usedSize--;
}
}
}
/*
清空顺序表
*/
public void clear() {
this.usedSize = 0;
}
}
3824





