package suanfa;
/**
*
* @author Administrator 顺序表在内存空间是一串连续的地址空间,最基础的线性表,随机存取 seqList
*/
public class SeqList {
private Integer[] array;
private int maxsize;
private int current = 0;
public SeqList(int n) {
if (n > 0) {
maxsize = n;
this.array = new Integer[n];
}
}
public boolean add(Integer n) {
if(current>maxsize){
return false;
}
array[current]= n;
current++;
return true;
}
public boolean delete(Integer location) {
if(location>=current){
return false;
}else{
for(int i= location;i<current-1;i++){
array[i]=array[i+1];
}
//写的时候忘记写这个了--记住
current--;
return true;
}
}
public Integer get(int n){
if(n<0|| n>=current){
return null;
}else{
return array[n];
}
}
public boolean update(int n,Integer m){
if(n<0|| n>=current){
return false;
}else{
array[n]=m;
return true;
}
}
public int getMaxsize() {
return maxsize;
}
public void setMaxsize(int maxsize) {
this.maxsize = maxsize;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
public static void main(String[] args) {
//建立顺序表
SeqList list = new SeqList(10);
for(int i=0;i<10;i++){
list.add(i*10);
}
//输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
删除索引3
list.delete(3);
//输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
//将顺序表索引为5的元素改为44
list.update(5, 55);
输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
}
}
/**
*
* @author Administrator 顺序表在内存空间是一串连续的地址空间,最基础的线性表,随机存取 seqList
*/
public class SeqList {
private Integer[] array;
private int maxsize;
private int current = 0;
public SeqList(int n) {
if (n > 0) {
maxsize = n;
this.array = new Integer[n];
}
}
public boolean add(Integer n) {
if(current>maxsize){
return false;
}
array[current]= n;
current++;
return true;
}
public boolean delete(Integer location) {
if(location>=current){
return false;
}else{
for(int i= location;i<current-1;i++){
array[i]=array[i+1];
}
//写的时候忘记写这个了--记住
current--;
return true;
}
}
public Integer get(int n){
if(n<0|| n>=current){
return null;
}else{
return array[n];
}
}
public boolean update(int n,Integer m){
if(n<0|| n>=current){
return false;
}else{
array[n]=m;
return true;
}
}
public int getMaxsize() {
return maxsize;
}
public void setMaxsize(int maxsize) {
this.maxsize = maxsize;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
public static void main(String[] args) {
//建立顺序表
SeqList list = new SeqList(10);
for(int i=0;i<10;i++){
list.add(i*10);
}
//输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
删除索引3
list.delete(3);
//输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
//将顺序表索引为5的元素改为44
list.update(5, 55);
输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
}
}