|
| 1 | +import java.util.Arrays; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by EKarpenko on 15.04.2015. |
| 5 | + */ |
| 6 | +public class ImprovedArray { |
| 7 | + private String[] array = new String[10]; |
| 8 | + private int arrayCounter = 0; |
| 9 | + |
| 10 | + public static void main(String[] args) { |
| 11 | + ImprovedArray improvedArray = new ImprovedArray(); |
| 12 | + improvedArray.add("4"); |
| 13 | + improvedArray.add("42"); |
| 14 | + improvedArray.add("1"); |
| 15 | + improvedArray.add("465"); |
| 16 | + improvedArray.add("38"); |
| 17 | + improvedArray.add("328"); |
| 18 | + improvedArray.add("82"); |
| 19 | + improvedArray.add("51"); |
| 20 | + improvedArray.add("118"); |
| 21 | + improvedArray.add("108"); |
| 22 | + improvedArray.add("19"); |
| 23 | + improvedArray.add("518"); |
| 24 | + |
| 25 | + String[] array2 = {"4", "42", "1", "465", "38", "328", "82", "51", "118", "108", "19", "518", |
| 26 | + null, null, null, null, null, null, null, null}; |
| 27 | + |
| 28 | + System.out.println(improvedArray.toString()); |
| 29 | + System.out.println(improvedArray.get(55)); |
| 30 | + System.out.println(improvedArray.size()); |
| 31 | + System.out.println(improvedArray.equals(array2)); |
| 32 | + } |
| 33 | + |
| 34 | + public void add(String value) { |
| 35 | + if (arrayCounter >= array.length) { |
| 36 | + array = Arrays.copyOf(array, array.length * 2); |
| 37 | + } |
| 38 | + |
| 39 | + array[arrayCounter++] = value; |
| 40 | + } |
| 41 | + |
| 42 | + public String get(int index) { |
| 43 | + return index < arrayCounter ? array[index] : null; |
| 44 | + } |
| 45 | + |
| 46 | + public int size() { |
| 47 | + return arrayCounter; |
| 48 | + } |
| 49 | + |
| 50 | + public boolean equals(Object other) { |
| 51 | + String[] otherArray = (String[]) other; |
| 52 | + |
| 53 | + if (array.length != otherArray.length) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + for (int i = 0; i < array.length; i++) |
| 58 | + { |
| 59 | + if (array[i] == null && otherArray[i] == null) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + if (array[i] == null || otherArray[i] == null || !array[i].equals(otherArray[i])) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + public String toString() { |
| 72 | + String string = "["; |
| 73 | + for (int i = 0; i < array.length; i++) |
| 74 | + { |
| 75 | + string += array[i] != null ? array[i] : " "; |
| 76 | + string += (i < array.length - 1 ? ", " : ""); |
| 77 | + } |
| 78 | + string += "]"; |
| 79 | + return string; |
| 80 | + } |
| 81 | +} |
0 commit comments