Examples of smaller Java programs that do various interesting things.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package Fall0811;
  2. import java.util.Iterator;
  3. public class GenericList<E> implements Iterable<E>{
  4. // class constant
  5. private static final int DEFAULT_CAP = 10;
  6. // instance variables
  7. protected E[] container; // the array is NOT the list
  8. private int listSize;
  9. public Iterator<E> iterator(){
  10. return new GenListIterator();
  11. }
  12. // inner class
  13. private class GenListIterator implements Iterator<E>{
  14. private int indexOfNextElement;
  15. private boolean okToRemove;
  16. private GenListIterator(){
  17. indexOfNextElement = 0;
  18. okToRemove = false;
  19. }
  20. public boolean hasNext(){
  21. return indexOfNextElement < size();
  22. }
  23. public E next(){
  24. assert hasNext();
  25. okToRemove = true;
  26. indexOfNextElement++;
  27. return container[indexOfNextElement - 1];
  28. }
  29. public void remove(){
  30. assert okToRemove;
  31. okToRemove = false;
  32. indexOfNextElement--;
  33. GenericList.this.remove(indexOfNextElement);
  34. }
  35. }
  36. public boolean equals(Object obj){
  37. assert this != null;
  38. if(obj == null)
  39. return false;
  40. else if (this == obj)
  41. return true;
  42. else if( this.getClass() != obj.getClass() )
  43. return false;
  44. else{
  45. // obj is a non null GenericList
  46. GenericList list = (GenericList)obj;
  47. if( list.size() != size() )
  48. return false;
  49. for(int i = 0; i < size(); i++)
  50. if( (get(i) == null && list.get(i) != null) || !get(i).equals(list.get(i)) )
  51. return false;
  52. return true;
  53. }
  54. }
  55. // creates an empty IntList
  56. public GenericList(){
  57. this(DEFAULT_CAP);
  58. }
  59. // pre: initialCap >= 0
  60. public GenericList(int initialCap){
  61. assert initialCap >= 0 : "failed precondition";
  62. container = (E[])(new Object[initialCap]);
  63. listSize = 0;
  64. }
  65. public void insertAll(int pos, GenericList<E> otherList){
  66. for(int i = 0; i < otherList.listSize; i++){
  67. this.insert(pos + i, otherList.container[i]);
  68. }
  69. }
  70. // pre: 0 <= pos < size()
  71. public E remove(int pos){
  72. E result = container[pos];
  73. listSize--;
  74. for(int index = pos; index < size(); index++){
  75. container[index] = container[index + 1];
  76. }
  77. container[listSize] = null;
  78. return result;
  79. }
  80. // pre: 0 <= pos <= size()
  81. public void insert(int pos, E element){
  82. assert 0 <= pos && pos <= size();
  83. if( size() == container.length )
  84. resize();
  85. for(int index = size(); index > pos; index--){
  86. assert index > 0;
  87. container[index] = container[index - 1];
  88. }
  89. container[pos] = element;
  90. listSize++;
  91. }
  92. // get size of list
  93. public int size(){
  94. return listSize;
  95. }
  96. // access elements
  97. // pre: 0 <= position < size()
  98. public E get(int position){
  99. assert 0 <= position && position < size();
  100. return container[position];
  101. }
  102. // pre: none
  103. public void add(E element){
  104. insert(size(), element);
  105. }
  106. private void resize() {
  107. E[] temp = (E[])(new Object[container.length * 2 + 1]);
  108. System.arraycopy(container, 0, temp, 0, size());
  109. container = temp;
  110. }
  111. public String toString(){
  112. StringBuffer result = new StringBuffer("[");
  113. final int LIMIT = size() - 1;
  114. for(int i = 0; i < LIMIT; i++){
  115. if( this == this.get(i) )
  116. result.append("this list");
  117. else{
  118. result.append(get(i));
  119. }
  120. result.append(", ");
  121. }
  122. if( size() != 0)
  123. result.append(get(size() - 1));
  124. result.append("]");
  125. return result.toString();
  126. }
  127. }