public class StringArray { /** This is the array of object. */ protected String[] itsArray; /** * Constructor of the object. <br> * * This constructor should remain private */ private StringArray() { super(); } /** * Constructor of the object. <br> * * @param aSize fixed length of the array */ public StringArray(int aSize) { super(); if (aSize < 0) throw new IllegalArgumentException("Size must be >=0"); this.itsArray = new String[aSize]; } /** * Constructor of the object. <br> * * @param anArray an array that will be use for the initialization of this array * @param doCopy if true will make a copy of the array given in parameter */ public StringArray(String[] anArray, boolean doCopy) { super(); this.itsArray = new String[anArray.length]; if (doCopy) { System.arraycopy(anArray, 0, this.itsArray, 0, anArray.length); } else { for (int i = 0; i < anArray.length; i++) { this.itsArray[i] = anArray[i]; } } } /** * Gives a string representation of this object. <br> * * @return a String representation of this object */ public String toString() { StringBuffer sb = new StringBuffer(128); sb.append('{'); for (int i = 0; i < this.itsArray.length; i++) { sb.append(this.itsArray[i]); sb.append(", "); } sb.delete(sb.length() - 2, sb.length()); sb.append('}'); return sb.toString(); } /** * Gives an element in the array. <br> * * @param anIndex index of the object to find * @return the object available at the given index */ public String get(int anIndex) { return this.itsArray[anIndex]; } /** * Sets the element in the array at the given index. <br> * * Warning: No copy or clone of the element is made. * * @param anIndex where to add the object * @param anElm the object to add */ public void set(String anElm, int anIndex) { this.itsArray[anIndex] = anElm; } /** * Gives the first place of the given element. <br> * * @param anElm the object to find * @return the index value of this element, or -1 if it is not present */ public int indexOf(String anElm) { return this.indexOf(anElm, 0); } /** * Gives the last place of the given element. <br> * * @param anElm the object to find * @return the last index value of this element, or -1 if it is not present */ public int lastIndexOf(String anElm) { int res = -1; for (int i = 0; i < this.itsArray.length; i++) { if (anElm == this.itsArray[i] || (anElm != null && anElm.equals(this.itsArray[i]))) { res = i; } } return res; } /** * Sorts the list. */ public void sort() { Arrays.sort(this.itsList); } /** * Sorts the lis with the help of a comparator. * * @param aComparator object that tells how to compare objets */ public void sort(Comparator aComparator) { Arrays.sort(this.itsList, aComparator); } /** * Gives the first place of the given element starting from a given position. <br> * * @param anElm the object to find * @param aStartIndex where to start searching * @return the index value of this element, or -1 if it is not present */ public int indexOf(String anElm, int aStartIndex) { int res = -1; for (int i = aStartIndex; i < this.itsArray.length && res == -1; i++) { if (anElm == this.itsArray[i] || (anElm != null && anElm.equals(this.itsArray[i]))) { res = i; } } return res; } /** * Clears the array. * * Puts null in each place of the array */ public void clear() { for (int i = 0; i < this.itsArray.length; i++) { this.itsArray[i] = null; } } /** * Deletes the array. * * Use this method for memory leak, the array will not be useable anymore */ public void delete() { this.clear(); this.itsArray = null; } /** * Returns the size of the array. * * @return the size of the array */ public int size() { return this.itsArray.length; } /** * Tests equality between two arrays. * * @param anObject the object compared to this * @return true if this is equals to the other object, false if not */ public boolean equals(Object anObject) { if (this == anObject) return true; if (null == anObject) return false; if (anObject instanceof StringArray) { StringArray o = (StringArray) anObject; if (o.size() != this.size()) return false; for (int i = 0; i < this.itsArray.length; i++) { if (this.itsArray[i] == o.itsArray[i] || (this.itsArray[i] != null && this.itsArray[i].equals(o.itsArray[i]))) { continue; } else { return false; } } return true; } return false; } /** * Tests if an element is present or not. <br> * * @param anElm the object to find * @return true if the element is present, false if not present */ public boolean contains(String anElm) { return indexOf(anElm) != -1; } /** * Makes a clone of this object. <br> * * @return a clone of the object */ public Object clone() { return new StringArray(this.itsArray, true); } /** * The main method. * * @param args arguments */ public static void main(String[] args) { /* StringArray anArray = new StringArray(5); for(int i=0; i<anArray.size(); i++) { anArray.set(i, new String()); } System.out.println(anArray); */ } }