📄 sequencelist.java
字号:
import java.awt.*;
public class SequenceList implements List
{
private Object[] entry; //保存元素的数组
private int length; //当前元素的个数
private static final int MAX_SIZE = 50; //数组大小
public void display()
{
for (int index = 0;index < length;index++)
System.out.println(entry[index]);
}
public SequenceList()
{
length = 0;
entry = new Object[MAX_SIZE];
}
public SequenceList(int maxSize)
{
length=0;
entry=new Object[maxSize];
}
public SequenceList(Object[] str)
{
entry=new Object[MAX_SIZE];
for(int i=0;i<str.length;i++)
{
entry[i]=str[i];
}
length=str.length;
}
public void clear() //置空表 求表长
{
length =0;
}
/*public void clear(SequenceList &T)
{
T.a=new elemtype[maxsize]; //动态申请存储单元
if(T.a==NULL) //申请不成功
exit(1);
T.len=0;
}*/
public int getLength() //求表长
{
return length;
}
public Object getEntry(int i) //取出第i个元素
{
Object result = null;
if ((i >=1)&&(i<=length))
result = entry[i-1];
return result;
}
public boolean contains(Object anEntry) //判断线性表是否包含元素anEntry
{
boolean found = false;
for (int index = 0;!found && (index < length);index++)
{
if(anEntry.equals(entry[index]))
found = true;
}
return found;
}
public boolean isFull()
{
return length == entry.length;
}
public boolean add(int i,Object newEntry) //将数据元素插入线性表中第i个元素之前
{
boolean isSuccessful = true;
if(entry.length ==length)
isSuccessful = false;
if(i < 1 || i > length +1)
isSuccessful = false;
for (int index = length;index >= i;index--)
entry[index] = entry[index -1];
entry[i -1] = newEntry;
++length;
return isSuccessful;
}
public Object remove(int i)
{
Object result = null;
if((i >=1) &&(i <= length))
{
result = entry[i-1];
for(int index = i; index < length; index++)
entry[index -1] = entry[index];
length = length -1;
}
return result;
}
public boolean add(Object newEntry)
{
boolean isSuccessful = true;
if(!isFull()){
entry[length] = newEntry;
length++;
}
else
isSuccessful = false;
return isSuccessful;
}
public boolean replace(int givenPosition, Object newEntry)
{
boolean isSuccessful = true;
if((givenPosition >= 1) && (givenPosition <= length))
entry[givenPosition-1] = newEntry;
else
isSuccessful = false;
return isSuccessful;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -