Untitled
unknown
plain_text
4 years ago
14 kB
17
Indexable
package cs1302.list;
import cs1302.listadt.StringList;
/** Represents an array-based {@link cs1302.listedadt.StringList}.
*/
public class ArrayStringList implements StringList {
// Instance variable for a String array named list
private String[] list;
private int size;
/**
* This is the default constructor for an Array-based string list.
* The initial size of a list object constructed using this constructor is zero,
* regardless of the list's underlying storage.
*/
public ArrayStringList() {
this.list = new String[5];
this.size = 0;
} // ArrayStringList
/**
* This is a copy constructor. The list object that is constructed using this constructer is
* a deep copy of the list reffered to by the constructor's parameter. It will contain the same
* elements in the same order.
* {@inheritDoc}
*/
public ArrayStringList(StringList other) {
if (other == null) {
throw new NullPointerException("The specified list is null.");
} else {
this.size = other.size();
for (int i = 0; i < other.size(); i++) {
add(other.get(i));
}
}
} // ArrayStringList copy constructor
/**
* Appends the strings in a string list to the end of the list.
* {@inheritDoc}
*/
public boolean add(StringList sl) {
boolean success = false;
if (sl == null) {
throw new NullPointerException("The specified list is null");
} else if (sl.size() == 0) {
return success;
}
success = true;
String[] tempList = new String[list.length + sl.size()];
int counter = 0;
// copy all elements over
for (int i = 0; i < size(); i++) {
tempList[i] = list[i];
counter++;
}
// adds to end of list
for (int i = 0; i < sl.size(); i++) {
tempList[counter] = sl.get(i);
counter++;
}
list = tempList;
size += sl.size();
return success;
}
/**
* Inserts a specified string at the specified position in this list. Shifts the string
* currently at that position and any subsequent strings to the right.
* {@inheritDoc}
*/
public boolean add(int index, String s) {
boolean success = false;
if (s == null) {
throw new NullPointerException("The specified string is null.");
} else if (s.isEmpty()) {
throw new IllegalArgumentException("The specified string is empty.");
} else if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException("The index is out of range");
} else {
success = true;
String [] tempList = new String[list.length + size()];
int counter = 0;
// add up to index
for (int i = 0; i <= index; i++) {
if (i != index) {
tempList[i] = list[i];
} else {
tempList[i] = s;
size++;
}
counter++;
}
for (int i = index; i < size(); i++) {
tempList[counter] = list[i];
counter++;
}
list = tempList;
return success;
}
} // add
/**
* Inserts the strings contained in the specified list at the position in this list,
* in the order in which they appear in the specified list.
* {@inheritDoc}
*/
public boolean add(int index, StringList sl) {
boolean success = false;
if (sl == null) {
throw new NullPointerException("The specified list is null");
} else if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException("The index is out of range");
} else if (sl.size() == 0) {
return success;
}
success = true;
//int number = list.length * 2 - 1;
//String[] tempList = new String[number];
String[] tempList = new String[list.length + sl.size()];
int counter = 0;
// Copys the list elements into the tempList until it reaches the index.
// When index is reached, add the string from StringList into it
for (int i = 0; i < index; i++) {
tempList[i] = list[i];
counter++;
}
for (int i = 0; i < sl.size(); i++) {
tempList[counter] = sl.get(i);
counter++;
}
for (int i = index; i < size(); i++) {
tempList[counter] = list[i];
}
size += sl.size();
list = tempList;
return success;
} // add with StringList
/**
* Appends the string to the end of the list.
* {@inheritDoc}
*/
public boolean add(String s) {
boolean success = false;
if (s == null) {
throw new NullPointerException("The specified string is null");
} else if (s.equals("")) {
throw new IllegalArgumentException("The specified string is empty");
}
success = true;
String[] tempList = new String[list.length + size()];
//Copy elements over into tempList until index.
for (int i = 0; i < size(); i++) {
tempList[i] = list[i];
}
tempList[size()] = s;
list = tempList;
size++;
return success;
} // add with string only
/**
* Clear all of the elements of the list.
* {@inheritDoc}
*/
public void clear() {
for (int i = 0; i < size(); i++) {
list[i] = null;
}
size = 0;
} // clear
/**
* Returns a true if list contains a specified string.
* {@inheritDoc}
*/
public boolean contains(String o) {
boolean contains = false;
if (o == null) {
throw new NullPointerException("The specified string is null");
} else if (o.equals("")) {
throw new IllegalArgumentException("The specified string is empty");
}
for (int i = 0; i < size(); i++) {
if (list[i].equals(o)) {
contains = true;
return contains;
}
}
return contains;
} // contains
/**
* Returns true if list contains a specified string regardless of case.
* {@inheritDoc}
*/
public boolean containsIgnoreCase(String o) {
boolean contains = false;
if (o == null) {
throw new NullPointerException("The specified string is null");
} else if (o.equals("")) {
throw new IllegalArgumentException("The specified string is empty");
}
for (int i = 0; i < size(); i++) {
if (list[i].equalsIgnoreCase(o)) {
contains = true;
return contains;
}
}
return contains;
} // containsIgnoreCase
/**
* Returns true if list contains a specified substring.
* {@inheritDoc}
*/
public boolean containsSubstring(String o) {
boolean contains = false;
if (o == null) {
throw new NullPointerException("The specified substring is null");
} else if (o.equals("")) {
throw new IllegalArgumentException("The specified substring is empty");
}
for (int i = 0; i < size(); i++) {
if (list[i].contains(o)) {
contains = true;
return contains;
}
}
return contains;
} // containsSubString
/**
* Builds and returns a StringList from the list without any duplicate strings.
* {@inheritDoc}
*/
public StringList distinct() {
StringList distinct = new ArrayStringList();
for (int i = 0; i < size(); i++) {
if (!distinct.contains(list[i])) {
distinct.add(list[i]);
}
}
return distinct;
} // distinct
/**
* Returns a string at a specific position.
* {@inheritDoc}
*/
public String get(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index is out of range");
} else {
return list[index];
}
} // get
/**
* Returns the index of the specified string.
* {@inheritDoc}
*/
public int indexOf(String s) {
if (s == null) {
throw new NullPointerException("The specified string is null");
} else if (s.equals("")) {
throw new IllegalArgumentException("The specified string is empty");
}
int indexOf = -1;
for (int i = 0; i < size(); i++) {
if (list[i].equals(s)) {
indexOf = i;
}
}
return indexOf;
} // indexOf
/**
* Returns the index of the specified string, ignoring case.
* {@inheritDoc}
*/
public int indexOfIgnoreCase(String s) {
if (s == null) {
throw new NullPointerException("The specified string is null");
} else if (s.equals("")) {
throw new IllegalArgumentException("The specified string is empty");
}
int indexOf = -1;
for (int i = 0; i < size(); i++) {
if (list[i].equalsIgnoreCase(s)) {
indexOf = i;
}
} // for
return indexOf;
} // indexOfIgnoreCase
/**
* Checks if the array is empty or not.
* {@inheritDoc}
*/
public boolean isEmpty() {
boolean isEmpty = false;
if (size() == 0) {
isEmpty = true;
return isEmpty;
}
return isEmpty;
} // isEmpty
/**
* Returns a string representation of the list.
* {@inheritDoc}
*/
public String makeString() {
String madeString = "";
for (int i = 0; i < size(); i++) {
madeString += list[i];
}
return madeString;
} // makeString
/**
* Returns a string representation of the list with a seperator string in between each element.
* {@inheritDoc}
*/
public String makeString(String sep) {
String madeString = "";
for (int i = 0; i < size(); i++) {
madeString += list[i] + sep;
}
return madeString;
} // makeString with String seperator
/**
* Returns a string representation of the list with a start, seperator, and end string.
* {@inheritDoc}
*/
public String makeString(String start, String sep, String end) {
String madeString = "";
String middleString = "";
for (int i = 0; i < size(); i++) {
if ((i != 0) && (i != size() - 1)) {
middleString += list[i] + sep;
}
}
madeString = start + list[0] + sep + middleString + list[size() - 1] + end;
return madeString;
}
/**
* Removes a string at a certain input and returns the string that was removed.
* {@inheritDoc}
*/
public String remove(int index) {
String removedString = list[index];
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("The index is out of bounds");
}
String[] newList = new String[list.length - 1];
for (int i = 0; i < index; i++) {
newList[i] = list[i];
}
for (int i = index; i < newList.length; i++) {
newList[i] = list[i + 1];
}
list = newList;
size--;
return removedString;
} // remove
/**
* Reverses the array elements and puts it into a StringList.
* {@inheritDoc}
*/
public StringList reverse() {
StringList reversedList = new ArrayStringList();
for (int i = size() - 1; i >= 0; i--) {
reversedList.add(get(i));
}
return reversedList;
} // reverse
/**
* Replaces the string at a specific index and returns the replaced string.
* {@inheritDoc}
*/
public String set(int index, String s) {
String removedString = list[index];
if (s == null) {
throw new NullPointerException("The specified string is null.");
} else if (s.equals("")) {
throw new IllegalArgumentException("The specified string is empty.");
} else if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("The index is out of range.");
} else {
list[index] = s;
return removedString;
} // else
} // set
/**
* Returns the number of elements in this list.
* @return the number of elements in this list.
*/
public int size() {
if (size > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return size;
} // size
/**
* Creates and returns a new StringList with elements from a specific index to a specific index.
* {@inheritDoc}
*/
public StringList splice(int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex > size() || fromIndex > toIndex) {
throw new IndexOutOfBoundsException("Illegal endpoint index value");
} else {
StringList newList = new ArrayStringList();
for (int i = fromIndex; i < toIndex; i++) {
newList.add(get(i));
}
return newList;
}
} // splice
/**
* Returns an array containing all of the strings in the list.
* {@inheritDoc}
*/
public String[] toArray() {
String[] newArray = new String[size()];
for (int i = 0; i < size(); i++) {
newArray[i] = list[i];
}
return newArray;
} // toArray
} // ArrayStringList
Editor is loading...