概述 线性表的顺序存储的基本方法是定义一个数组类型的对象来存储集合元素,同时还要定义一个整型对象来存储集合的长度,以及定义一个整型常量对象来保存待定义数组的初始长度。
1 2 3 final int maxSize=20 ; private Object setArray[]; private int length;
操作实现 1.初始化 通过构造函数,初始化集合为空,并保存集合的数组setArray定义对象。构造函数有两个,一个无参构造函数,另一个为带有数组初始长度参数的构造函数。定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 public Set () { length=0 ; setArray=new Object[maxSize]; } public Set (int n) { if (n<=0 ){ System.out.println("数组长度必须大于0!" ); System.exit(1 ); } length=0 ; setArray=new Object[n]; }
2.向集合插入一个元素 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public boolean add (Object ob,int n) { if (n<1 ||n>length+1 ){ System.out.println("参数不合法!" ); return false ; } if (length==setArray.length){ Object b[]=new Object[length*2 ]; for (int i=0 ;i<length;i++) b[i]=setArray[i]; setArray=b; } for (int i=length;i>=n;i--) setArray[i]=setArray[i-1 ]; setArray[n-1 ]=ob; length++; return true ; }
3.从集合里删除一个元素 1 2 3 4 5 6 7 8 9 10 public boolean remove (Object ob) { int i; for (i=0 ;i<length;i++) if (setArray[i].equals(ob)) break ; if (i<length){ setArray[i]=setArray[length-1 ]; length--; return true ; }else return false ; }
4.判断一个元素是否属于集合 1 2 3 4 5 public boolean contains (Object ob) { for (int i=0 ;i<length;i++) if (setArray[i].equals(ob)) return true ; return false ; }
5.返回集合第i个元素的值 1 2 3 4 5 6 7 public Object value (int i) { if (i<=0 ||i>length){ System.out.println("i应该在1和" +length+"之间" ); return null ; } return setArray[i-1 ]; }
6.从集合中按值查找元素 1 2 3 4 5 public Object find (Object ob) { for (int i=0 ;i<length;i++) if (setArray[i].equals(ob)) return setArray[i]; return null ; }
7.返回集合的长度 1 public int size () {return length;}
8.判断集合是否为空 1 public boolean isEmpty () {return length==0 ;}
9.输出集合中所有元素 1 2 3 4 5 public void output () { for (int i=0 ;i<length;i++) System.out.print(setArray[i].toString()+" " ); System.out.println(); }
10.求两个集合的并集 1 2 3 4 5 6 7 8 9 10 public Set union (Set set) { Set setTemp=new Set(setArray.length); int i; for (i=0 ;i<length;i++) setTemp.setArray[i]=setArray; setTemp.length=length; for (i=0 ;i<set.length;i++) setTemp.add(set.setArray[i]); return setTemp; }
11.求两个集合的交集 1 2 3 4 5 6 7 8 9 public Set intersection (Set set) { int x; if (length<set.length) x=length;else x=set.length; Set setTemp=new Set(x); for (int i=0 ;i<set.length;i++) if (contains(set.setArray[i])) setTemp.setArray[setTemp.length++]=set.setArray[i]; return setTemp; }
12.清除集合中所有元素 1 public void clear () {length=0 ;}
实现代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 public class Set { final int maxSize=20 ; private Object setArray[]; private int length; public Set () { length=0 ; setArray=new Object[maxSize]; } public Set (int n) { if (n<=0 ){ System.out.println("数组长度必须大于0!" ); System.exit(1 ); } length=0 ; setArray=new Object[n]; } public boolean add (Object ob) { for (int i=0 ;i<length;i++) if (setArray[i].equals(ob)) return false ; if (length==setArray.length){ Object b[]=new Object[length*2 ]; for (int i=0 ;i<length;i++) b[i]=setArray[i]; setArray=b; } setArray[length]=ob; length++; return true ; } public boolean remove (Object ob) { int i; for (i=0 ;i<length;i++) if (setArray[i].equals(ob)) break ; if (i<length){ setArray[i]=setArray[length-1 ]; length--; return true ; }else return false ; } public boolean contains (Object ob) { for (int i=0 ;i<length;i++) if (setArray[i].equals(ob)) return true ; return false ; } public Object value (int i) { if (i<=0 ||i>length){ System.out.println("i应该在1和" +length+"之间" ); System.exit(1 ); } return setArray[i-1 ]; } public Object find (Object ob) { for (int i=0 ;i<length;i++) if (setArray[i].equals(ob)) return setArray[i]; return null ; } public int size () {return length;} public boolean isEmpty () {return length==0 ;} public void output () { for (int i=0 ;i<length;i++) System.out.print(setArray[i].toString()+" " ); System.out.println(); } public Set union (Set set) { Set setTemp=new Set(setArray.length); int i; for (i=0 ;i<length;i++) setTemp.setArray[i]=setArray[i]; setTemp.length=length; for (i=0 ;i<set.length;i++) setTemp.add(set.setArray[i]); return setTemp; } public Set intersection (Set set) { int x; if (length<set.length) x=length;else x=set.length; Set setTemp=new Set(x); for (int i=0 ;i<set.length;i++) if (contains(set.setArray[i])) setTemp.setArray[setTemp.length++]=set.setArray[i]; return setTemp; } public void clear () {length=0 ;} }