python顺序表基本操作_Java-实现顺序表的基本操作

List.java

package com.wanali.java_ds.sqlist;

public interface List {

public boolean empty();

public void clear();

public int length();

public Object get(int i);

public void insert(int i, Object x) throws Exception;

public Object delete(int i);

public void display();

SqList.java

package com.wanali.java_ds.sqlist;

public class SqList implements List {

private Object[] elem;

private int curlength;

public SqList(int maxsize) {

curlength = 1;

elem = new Object[maxsize];

}

public boolean empty() {

return curlength == 0;

}

public int length() {

// TODO Auto-generated method stub

return curlength;

}

public void clear() {

curlength = 0;

System.out.println("表已经清空了!!!");

}

public Object get1(int i) {

return elem[i - 1];

}

public void insert(int i, Object x) throws Exception {

if (i < 0 || i > curlength)

throw new Exception("输入的参数不合法");

int maxsize = 10;

if (curlength == elem.length)

elem[maxsize] = elem[2 * maxsize];

for (int j = curlength; j > i; j--)

elem[j] = elem[j - 1];

elem[i] = x;

++curlength;

}

public Object delete(int i) {

for (int j = i; j < curlength; j++)

elem[j] = elem[j + 1];

--curlength;

return elem[i];

}

public void display() {

// TODO Auto-generated method stub

for (int i = 1; i < curlength; i++)

System.out.println(elem[i]);

}

public Object get(int i) {

// TODO Auto-generated method stub

System.out.println(elem[i]);

return null;

}

}

TestSqList.java

package com.wanali.java_ds.sqlist;

public class TestSqList {

public static void main(String[] args) {

SqList sqList = new SqList(10);

try {

sqList.insert(1, 10);

sqList.insert(2, 20);

sqList.insert(3, 30);

sqList.insert(4, 40);

System.out.println("按顺序输出表中的元素:");

sqList.display();

System.out.println("删除一个元素:");

sqList.delete(2);

sqList.display();

System.out.println("获得第i个元素:");

sqList.get(2);

System.out.println("清空顺序表:");

sqList.clear();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

运行结果如下:

相关资源:顺序基本操作
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页