java语言实现顺序表
来源:互联网 发布:剑灵大地力士卡刀数据 编辑:程序博客网 时间:2024/05/16 14:29
/**
* 说明:这是一个用数组实现的顺序表,实现了基本的增、删、查操作。
*/
public class SequenList {
public int length=10;//初始化顺序表大小为10
private int cur_index;//当前插入位置
private Object[] seqList;//顺序表数据存放处
/**
* @author weixm
* 说明:构造函数,用来初始化顺序表。此处用私有方法来对数据进行初始化。
*/
public SequenList(){
initList();
}
/**
* @author weixm
* 初始化顺序表
*/
private void initList(){
this.seqList=new Object[length];
}
/**
* @author weixm
* @param obj
* 说明:此方法用来在顺序表中最后位置添加新数据,当数据添加位置超过了数组允许大小时,
* 重新分配内存空间。并且,每加一条数据,List的当前插入位置自增“1”。
*/
public void add(Object obj){
if(length>0){
if(cur_index<(length-1)){
seqList[cur_index]=obj;
cur_index++;
}else{
resizeList(seqList);
seqList[cur_index]=obj;
cur_index++;
}
}else{
System.err.print("The sequences are not initialed properly ! ");
}
}
/**
* @author weixm
* @param index
* @param obj
* 说明:此方法用来在数组的任意位置添加一个新数据。如果任意插入的数据位置刚好是最后一个元素
* 则直接调用 add(Object obj) 方法,如果添加的数据超出了原始数据的边界值,则List
* 会自动对空间进行扩充。因此,允许添加数据的当前位置超出数据边界。但是每次插入的数据
* 索引大小不能超过 length*3/2+1 ,否则,系统会抛出数组越界异常。
*/
public void add(int index,Object obj){
if(index==cur_index+1){
add(obj);
}else{
int old_len=length;
if(index>length-1){
resizeList(seqList);
}
System.arraycopy(seqList,index,seqList,index+1,old_len-index);
seqList[index]=obj;
cur_index=index;
}
}
/**
* @author weixm
* @param obj
* @return
* 说明:此方法用于进行元素查找,如果要查找的元素在数据中存在,则返回数据所在位置,如果
* 元素在数据中没有找到,则直接返回 -1 。
*/
public int findElement(Object obj){
int find_index=-1;
for(int i=0;i<length;i++){
if(obj.equals(seqList[i])){
find_index=i;
}
}
return find_index;
}
/**
* @author weixm
* @param index
* @return
* 说明:此方法用来得到某个确定位置的元素。
*/
public Object get(int index){
return seqList[index];
}
/**
* @author weixm
* @param seqList_low
* 说明:重新划分并扩充数据所占内存空间,具体扩充方式为: length*3/2+1 。
*/
public void resizeList(Object[] seqList_low){
int resize=length*3/2+1;
Object[] seqList_upp=new Object[resize];
System.arraycopy(seqList_low,0,seqList_upp,0,length);
seqList=seqList_upp;
length=resize;
}
*/
public class SequenList {
public int length=10;//初始化顺序表大小为10
private int cur_index;//当前插入位置
private Object[] seqList;//顺序表数据存放处
/**
* @author weixm
* 说明:构造函数,用来初始化顺序表。此处用私有方法来对数据进行初始化。
*/
public SequenList(){
initList();
}
/**
* @author weixm
* 初始化顺序表
*/
private void initList(){
this.seqList=new Object[length];
}
/**
* @author weixm
* @param obj
* 说明:此方法用来在顺序表中最后位置添加新数据,当数据添加位置超过了数组允许大小时,
* 重新分配内存空间。并且,每加一条数据,List的当前插入位置自增“1”。
*/
public void add(Object obj){
if(length>0){
if(cur_index<(length-1)){
seqList[cur_index]=obj;
cur_index++;
}else{
resizeList(seqList);
seqList[cur_index]=obj;
cur_index++;
}
}else{
System.err.print("The sequences are not initialed properly ! ");
}
}
/**
* @author weixm
* @param index
* @param obj
* 说明:此方法用来在数组的任意位置添加一个新数据。如果任意插入的数据位置刚好是最后一个元素
* 则直接调用 add(Object obj) 方法,如果添加的数据超出了原始数据的边界值,则List
* 会自动对空间进行扩充。因此,允许添加数据的当前位置超出数据边界。但是每次插入的数据
* 索引大小不能超过 length*3/2+1 ,否则,系统会抛出数组越界异常。
*/
public void add(int index,Object obj){
if(index==cur_index+1){
add(obj);
}else{
int old_len=length;
if(index>length-1){
resizeList(seqList);
}
System.arraycopy(seqList,index,seqList,index+1,old_len-index);
seqList[index]=obj;
cur_index=index;
}
}
/**
* @author weixm
* @param obj
* @return
* 说明:此方法用于进行元素查找,如果要查找的元素在数据中存在,则返回数据所在位置,如果
* 元素在数据中没有找到,则直接返回 -1 。
*/
public int findElement(Object obj){
int find_index=-1;
for(int i=0;i<length;i++){
if(obj.equals(seqList[i])){
find_index=i;
}
}
return find_index;
}
/**
* @author weixm
* @param index
* @return
* 说明:此方法用来得到某个确定位置的元素。
*/
public Object get(int index){
return seqList[index];
}
/**
* @author weixm
* @param seqList_low
* 说明:重新划分并扩充数据所占内存空间,具体扩充方式为: length*3/2+1 。
*/
public void resizeList(Object[] seqList_low){
int resize=length*3/2+1;
Object[] seqList_upp=new Object[resize];
System.arraycopy(seqList_low,0,seqList_upp,0,length);
seqList=seqList_upp;
length=resize;
}
0 0
- java语言实现顺序表
- java语言实现顺序表
- C语言实现顺序表
- 顺序表c语言实现
- c语言实现顺序表
- 顺序表(C++语言实现)
- C语言实现顺序表
- C语言实现顺序表
- C语言实现顺序表
- C语言实现顺序表
- C语言实现顺序表
- C语言实现顺序表
- C语言实现顺序表
- c语言实现顺序表
- C语言实现顺序表
- C语言实现顺序表
- C语言实现顺序表
- C语言实现顺序表
- Implement strStr()--LeetCode
- 版本控制的策划书
- matlab中ezplot和plot, fplot这3
- JAVA组件使用---UUID使用方法
- python子进程调用casperjs中文乱码
- java语言实现顺序表
- ZOJ3331---Process the Tasks(dp)
- python 实践程序1——实现binaryproto文件到npy文件的转化
- 优先队列的一种使用方法 bjfuoj1310
- Aspose.Email.Imap 使用
- So Easy!7个立竿见影的EDM转化方法
- C++继承关系中Data Member和Functions Member的内存布局(基于C++ Object Model描述)
- 服务器开发之六 对Makefile文件进行抽象
- Wininet https问题