您的位置:首页 > 编程语言 > Java开发

java 实现线性表之顺序存储

2014-08-16 16:58 309 查看
package com.zhiru;

public class MyArrayList {
private int[]data;
private int length;
private int maxLength=100;
MyArrayList(int size){
if(size<100){
length=0;
data=new int[size];
}
else{
System.out.println("size is too big");
}
}
//init the arraylist with the array a
MyArrayList(int []a){
length=0;
data=new int[maxLength];
if(a.length<maxLength){
for(int i=0;i<a.length;i++){
data[length++]=a[i];
}
}
else{
System.out.println("size is too big");
}

}
public int getLength(){
return length;
}
public void fill(int val){
data[length++]=val;
}
//set the value on the pos position.
public void set(int pos,int val){
if(pos>0&&pos<length)
data[pos-1]=val;
}
public int get(int pos){
if(pos>0&&pos<length){
return data[pos-1];
}
return 0;
}
public void printArray(){
if(length>0){
for(int j=0;j<length;j++){
System.out.print(data[j]+" ");
}
System.out.print("\n");
}
}
public boolean deleteValByPos(int pos){
boolean o=false;
if(pos>0&&pos<length+1){
for(int j=pos-1;j<length-1;j++){
//cong kaishi shanchu de wei zhi de hou yi wei kai shi
//wang qian fu gai qianmian de zhi.
data[j]=data[j+1];
}
length--;
o=true;
}
return o;

}
public boolean deleteValByVal(int val){
int m;
for( m=0;m<length;m++){
if(data[m]==val)
break;
}
// still use the method deleteValByPos to delete the val.
return deleteValByPos(m+1);
}
public boolean insertToArray(int pos,int val){
boolean o=false;
if(pos<1||pos>length){
System.err.print("wrong");
} else{
for(int j=length;j>=pos+1;j--){
data[j]=data[j-1];
}
length++;
data[pos]=val;
o=true;
}
return o;
}
//if the data is empty
public boolean isEmpty(){
return length==0;
}
//return the position of the value given
//position=[0,length-1]
public int Locate(int val){
int i=0;
while(i<=length){
if(val==data[i])
break;
else
i++;
}
return i;
}
// decide whether the value is the data[] member.
//exists true,else false.
public boolean isExists(int val){
boolean o=false;
int index=Locate(val);
if(index>length)
o=false;
else
o=true;
return o;
}
public void jiaoJi(MyArrayList ml){
//i is the index of this object.
//j is the index of ml's object
int []x=new int[((this.length+1)<(ml.length+1)?(this.length+1):(ml.length+1))];
int i=0,index=0;
while(i<=this.length){
if(ml.isExists(this.data[i])){
x[index++]=this.data[i];
}
i++;
}
System.out.print("交集为:\n");
for(int ind=0;ind<index;ind++)
System.out.print(x[ind]+" ");
System.out.print("\n");
}
/*
* input:MyArrayList object
* output:they are union.
*/
public void binJi(MyArrayList ml){
int mLen=ml.length+1;
int j=0;
while(j<mLen){
if(this.isExists(ml.data[j])==false){
this.insertToArray(this.length, ml.data[j]);
}
j++;
}
System.out.print("并集为:\n");
for(int i=0;i<this.length+1;i++){
System.out.print(this.data[i]+" ");
}
System.out.print("\n");
}

}

package com.zhiru;

public class TestArrayList {

public static void main(String[] args) {
// TODO Auto-generated method stub
MyArrayList mal=new MyArrayList(20);
MyArrayList mal1=new MyArrayList(20);
int []a={1,2,5,6,7,8};
MyArrayList mal2=new MyArrayList(a);
for(int i=0;i<10;i++){
mal.fill(i+1);
mal1.fill(i+2);
}
mal.printArray();
mal1.printArray();
mal.jiaoJi(mal1);
mal.binJi(mal1);
mal2.printArray();
System.out.println(mal2.getLength());
mal2.insertToArray(1, 12);
mal2.insertToArray(mal2.getLength(), 100);
mal2.printArray();
mal2.deleteValByPos(1);
mal2.printArray();
mal2.deleteValByPos(7);
mal2.printArray();
//        mal2.insertToArray(6, 20);
}

}


1 2 3 4 5 6 7 8 9 10 

2 3 4 5 6 7 8 9 10 11 

交集为:

2 3 4 5 6 7 8 9 10 0 

并集为:

1 2 3 4 5 6 7 8 9 10 11 0 

1 2 5 6 7 8 

6

1 12 2 5 6 7 8 100 

12 2 5 6 7 8 100 

12 2 5 6 7 8 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息