?? sequencelist.java
字號(hào):
//2. 接口實(shí)現(xiàn)及測(cè)試
//SequenceList.java
public class SequenceList implements List{
private int max=-1;
private int current=-1;
private Object[] objectArray;
public SequenceList(int a){
max = a;
initList();
}
public void initList(){
objectArray = new Object[max];
current = 0;
}
public void clear(){
objectArray = new Object[max];
current = -1;
}
public int getLength(){
return current+1;
}
public Object getEntry(int i){
if(i >current || i <0){
return null;
}
return objectArray[i];
}
public int contains(Object anEntry){
int flag=0;
for(int i = 0;i<current;i++){
if(anEntry.equals(objectArray[i])){
flag=0;
return i+1;
}
else
flag=1;
}
if(flag==0)
System.out.println("元素"+anEntry+"存在!");
else
System.out.println("元素"+anEntry+"不存在!");
return -1;
}
public boolean isFull(){
if(current==max){
System.out.println("鏈表已滿!");
return true;
}
else
{System.out.println("鏈表未滿!");
return false;}
}
public boolean add(int i, Object newEntry){
if(i > current || i <0){
return false;
}
int j = current+1;
while(j>i){
objectArray[j] =objectArray[j-1];
j--;
}
objectArray[i] = newEntry;
current++;
return true;
}
public Object remove(int i){
if(i > current || i <0){
return false;
}
while(i<current){
objectArray[i] =objectArray[i+1];
i++;
}
current--;
return true;
}
public boolean add(Object newEntry){
current=current+1;
objectArray[current] =newEntry;
return true;
}
public boolean replace(int givenPosition, Object newEntry){
if(givenPosition>current||givenPosition<0){
return false;
}
for(int i=0;i<current;i++){
if(i==givenPosition)
objectArray[i]= newEntry;
}
return true;
}
public static void main(String[] args){
String aa="aa";
String bb="bb";
String cc="cc";
String dd="dd";
String ee="ee";
String ff="ff";
String gg="gg";
String hh="hh";
String ii="ii";
SequenceList L=new SequenceList(10);
L.add(aa);
L.add(bb);
L.add(dd);
L.add(ee);
L.add(ff);
L.add(gg);
L.add(hh);
System.out.println("原表為:");
for(int i=1;i<L.getLength();i++)
System.out.println(L.getEntry(i));
L.add(3, cc);
System.out.println("插入元素后的表為:");
for(int i=1;i<L.getLength();i++)
System.out.println(L.getEntry(i));
System.out.println("表長(zhǎng)="+L.getLength());
System.out.println("第二個(gè)元素的值為="+L.getEntry(2));
L.contains(ii);
L.isFull();
L.remove(6);
System.out.println("刪除元素后的表為:");
for(int i=1;i<L.getLength();i++)
System.out.println(L.getEntry(i));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -