Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package typinggame;
- // A String[] that doubles in size when full.
- class GrowingStringArray {
- private static String[] array;
- private int putloc;
- public static int length;
- // Handles String[] argument.
- GrowingStringArray(String[] a) {
- array = a;
- putloc = 0;
- length = a.length;
- }
- // Handles new String[] with size.
- GrowingStringArray(int size) {
- array = new String[size];
- putloc = 0;
- length = size;
- }
- // Handles default GrowingStringArray.
- GrowingStringArray() {
- array = new String[1];
- putloc = 0;
- length = 1;
- }
- // Doubles the size of the array.
- private static void grow() {
- String[] temp = array;
- array = new String[temp.length * 2];
- length = temp.length * 2;
- for(int i=0; i < temp.length; i++) {
- array[i] = temp[i];
- }
- }
- // Tries to put String in array; doubles if unsuccessful.
- public void put(String s) {
- if(putloc != length) {
- array[putloc++] = s;
- } else {
- grow();
- }
- }
- // Gets String[].
- public String[] get() {
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement