[关闭]
@ysner 2021-09-13T12:44:50.000000Z 字数 2208 阅读 765

燕山楠Homework 0

数据结构 JAVA


程序源码

一些尝试:
1、学习使用JAVA的输入语句,减少程序修改次数。
2、throw new IllegalArgumentException("")可以自定义报错显示+直接终止程序。

  1. import java.util.Scanner;
  2. public class Hw1_12
  3. {
  4. static class Set<E>
  5. {
  6. final int Mxsize=100;
  7. E[] data;
  8. int size;
  9. public Set()
  10. {
  11. data=(E[])new Object[Mxsize];
  12. size=0;
  13. }
  14. public int getsize()
  15. {
  16. return size;
  17. }
  18. public E get(int i)
  19. {
  20. if(i<0||i>=size)
  21. throw new IllegalArgumentException("failing get: I is not in the right range!");
  22. return (E)data[i];
  23. }
  24. public boolean IsIn(E e)
  25. {
  26. for(int i=0;i<size;i++)
  27. if(data[i]==e) return true;
  28. return false;
  29. }
  30. public boolean add(E e)
  31. {
  32. if(IsIn(e)||size==Mxsize) return false;
  33. else
  34. {
  35. data[size]=e;
  36. size++;
  37. return true;
  38. }
  39. }
  40. public boolean delete(E e)
  41. {
  42. int i=0;
  43. while(i<size&&data[i]!=e) i++;
  44. if(i>=size) return false;
  45. for(int j=i+1;j<size;j++) data[j-1]=data[j];
  46. size--;
  47. return true;
  48. }
  49. public void display()
  50. {
  51. for(int i=0;i<size;i++)
  52. if(i==0) System.out.print(data[i]);
  53. else System.out.print(" "+data[i]);
  54. System.out.println();
  55. }
  56. }
  57. static class TwoSet<E>
  58. {
  59. public Set<E> Union(Set<E> s1,Set<E> s2)
  60. {
  61. Set<E> s3=new Set<E>();
  62. for(int i=0;i<s1.getsize();i++) s3.add(s1.get(i));
  63. for(int i=0;i<s2.getsize();i++)
  64. if(!s1.IsIn(s2.get(i))) s3.add(s2.get(i));
  65. return s3;
  66. }
  67. public Set<E> Intersection(Set<E> s1,Set<E> s2)
  68. {
  69. Set<E> s3=new Set<E>();
  70. for(int i=0;i<s1.getsize();i++)
  71. if(s2.IsIn(s1.get(i))) s3.add(s1.get(i));
  72. return s3;
  73. }
  74. public Set<E> Difference(Set<E> s1,Set<E> s2)
  75. {
  76. Set<E> s3=new Set<E>();
  77. for(int i=0;i<s1.getsize();i++)
  78. if(!s2.IsIn(s1.get(i))) s3.add(s1.get(i));
  79. return s3;
  80. }
  81. }
  82. public static void main(String[] args)
  83. {
  84. Set<Integer> s1,s2,s3,s4,s5;
  85. TwoSet<Integer> t=new TwoSet<Integer>();
  86. s1=new Set<Integer>();
  87. Scanner in=new Scanner(System.in);
  88. System.out.println("Please enter the size of Set s1: ");
  89. int n=in.nextInt();
  90. System.out.println("Please enter the elements of Set s1: ");
  91. while(n>0) {s1.add(in.nextInt());n--;}
  92. s2=new Set<Integer>();
  93. System.out.println("Please enter the size of Set s2: ");
  94. n=in.nextInt();
  95. System.out.println("Please enter the elements of Set s2: ");
  96. while(n>0) {s2.add(in.nextInt());n--;}
  97. s3=t.Union(s1,s2);
  98. System.out.println("s3 is the union of s1 and s2!");
  99. System.out.print("Set s3: ");s3.display();
  100. s4=t.Difference(s1,s2);
  101. System.out.println("s4 is the difference set of s1 and s2!");
  102. System.out.print("Set s4: ");s4.display();
  103. System.out.println("s5 is the intersection of s1 and s2!");
  104. s5=t.Intersection(s1,s2);
  105. System.out.print("Set s5: ");s5.display();
  106. s1.get(5);//看看自己写的边界检查语句效果怎么样
  107. }
  108. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注