fork download
  1. public class ListTester {
  2. public static void main(String[] args) {
  3. ListOfStrings list = new ListOfStrings();
  4.  
  5. list.add("Doreen");
  6. list.add("John");
  7. list.add("Dan");
  8. list.add("Tim");
  9. list.add("Elaine");
  10. list.add("Jack");
  11.  
  12. System.out.println(list);
  13. list.add("Sheba");
  14. System.out.println(list);
  15.  
  16.  
  17. System.out.println(list);
  18. list.removeFirst();
  19. System.out.println(list);
  20. list.removeLast();
  21. System.out.println(list);
  22. list.ensureCapacity(13);
  23. System.out.println(list);
  24. list.compress();
  25. System.out.println(list);
  26. list.getIndex("John");
  27.  
  28.  
  29.  
  30.  
  31. }
  32. }
  33.  
  34.  
  35. import java.util.Arrays;
  36.  
  37. public class ListOfStrings {
  38. public static final int DEFAULT_CAPACITY = 6;
  39. private String[] names = new String[DEFAULT_CAPACITY];
  40. private int size = 0;
  41.  
  42.  
  43. public ListOfStrings(){
  44.  
  45. }
  46.  
  47. public int getCapacity(){
  48. return names.length;
  49. }
  50.  
  51. public int getSize(){
  52. return size;
  53. }
  54.  
  55. public void add(String name){
  56. if(this.getSize() == this.getCapacity()){
  57. // double the array size
  58. String[] temp = new String[this.getCapacity() * 2];
  59. System.arraycopy(names, 0, temp, 0, names.length);
  60. names = temp;
  61. }
  62. names[size] = name;
  63. size++;
  64. }
  65.  
  66. public void set(int i, String name){
  67. if(i < 0 || i > getSize() - 1){
  68. String message = "index " + i + " not valid";
  69. throw new IndexOutOfBoundsException(message);
  70. }
  71. names[i] = name;
  72. }
  73.  
  74. public String remove(int removeIndex){
  75. if(removeIndex < 0 || removeIndex > getSize() - 1){
  76. String message = "index " + removeIndex + " not valid";
  77. throw new IndexOutOfBoundsException(message);
  78. }
  79.  
  80. String removedItem = names[removeIndex]; // Save
  81. for(int i = removeIndex; i <= size - 2; i++){
  82. names[i] = names[i + 1];
  83. }
  84. names[size - 1] = null;
  85. size--;
  86. return removedItem;
  87. }
  88.  
  89. public String get(int i){
  90. if(i < 0 || i > getSize() - 1){
  91. String message = "index " + i + " not valid";
  92. throw new IndexOutOfBoundsException(message);
  93. }
  94. return names[i];
  95. }
  96.  
  97. public String removeFirst(){
  98. String temp = names[0];
  99. remove(0);
  100. return temp;
  101. }
  102.  
  103. public String removeLast(){
  104. String temp = names[0];
  105. remove(size - 1);
  106. return temp;
  107. }
  108.  
  109. public void compress(){
  110. String[] temp = new String[size];
  111. System.arraycopy(names, 0, temp, 0, size);
  112. names = temp;
  113. }
  114.  
  115. public void ensureCapacity(int newCapacity) {
  116. if(newCapacity > this.getCapacity()){
  117. String[] temp = new String[newCapacity];
  118. System.arraycopy(names, 0, temp, 0, names.length);
  119. names = temp;
  120. }
  121. }
  122.  
  123. public int getIndex(String s){
  124. int index = 1;
  125. for(int i = 0; (i < names.length); i++){
  126. if(names[i].equals(s)){
  127. index = i;
  128. }
  129. }
  130. return index;
  131. }
  132.  
  133. public String remove(String s)
  134. {
  135. String removedItem = names[getIndex(s)]; // Save
  136. for(int i = getIndex(s); i <= size - 2; i++){
  137. names[i] = names[i + 1];
  138. }
  139. names[size - 1] = null;
  140. size--;
  141. return removedItem;
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. public void Clear(){
  149. names = new String[DEFAULT_CAPACITY];
  150. }
  151. @Override
  152. public String toString(){
  153. String s = "";
  154. s += "capacity: " + this.getCapacity();
  155. s += ". size: " + this.getSize();
  156. s += ".\n";
  157. s += "[";
  158. boolean first = true;
  159. for(int i = 0; i < size; i++){
  160. if(first){
  161. s+= names[i];
  162. first = false;
  163. } else {
  164. s+= ", " + names[i];
  165. }
  166. }
  167. s += "]";
  168.  
  169. return s;
  170. }
  171.  
  172. }
  173.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:35: error: class, interface, or enum expected
import java.util.Arrays;
^
1 error
stdout
Standard output is empty