Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package typinggame;
  2.  
  3. // A String[] that doubles in size when full.
  4. class GrowingStringArray {
  5. private static String[] array;
  6. private int putloc;
  7. public static int length;
  8.  
  9. // Handles String[] argument.
  10. GrowingStringArray(String[] a) {
  11. array = a;
  12. putloc = 0;
  13. length = a.length;
  14. }
  15.  
  16. // Handles new String[] with size.
  17. GrowingStringArray(int size) {
  18. array = new String[size];
  19. putloc = 0;
  20. length = size;
  21. }
  22.  
  23. // Handles default GrowingStringArray.
  24. GrowingStringArray() {
  25. array = new String[1];
  26. putloc = 0;
  27. length = 1;
  28. }
  29.  
  30. // Doubles the size of the array.
  31. private static void grow() {
  32. String[] temp = array;
  33. array = new String[temp.length * 2];
  34. length = temp.length * 2;
  35.  
  36. for(int i=0; i < temp.length; i++) {
  37. array[i] = temp[i];
  38. }
  39. }
  40.  
  41. // Tries to put String in array; doubles if unsuccessful.
  42. public void put(String s) {
  43. if(putloc != length) {
  44. array[putloc++] = s;
  45. } else {
  46. grow();
  47. }
  48. }
  49.  
  50. // Gets String[].
  51. public String[] get() {
  52. return array;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement