Package cs1302.oracle
Class OracleStringList
java.lang.Object
cs1302.oracle.OracleStringList
- All Implemented Interfaces:
StringList
Oracle implementation of a
StringList. For the purposes of
your project, an object of this OracleStringList class is called an
oracle. Some examples that illustrate how you might use
an oracle can be found
here.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(int index, StringList itemList) Inserts multiple items into this string list at the specified index position.booleanInserts an item into this string list at the specified index position.voidclear()Removes all of the items from this string list.booleanReturnstrueifstart >= 0and there exists an item at or after thestartindex that equals thetargetstring.get(int index) Returns the item at the specified index position in this string list.booleanisEmpty()Returnstrueif this string list has no items.makeString(String start, String sep, String end) Returns a string representation of this string list that begins withstartand ends withend, with every string in the string list separated bysep.remove(int index) Removes the item at the specified index position in this string list.reverse()Returns a new string list that contains the items from this string list in reverse order.intsize()Returns the number of items in this string list.slice(int start, int stop) Returns a new string list that contains the items from this list between the specifiedstartindex (inclusive) andstopindex (exclusive).toString()ReturnsmakeString("[", ", ", "]").
-
Constructor Details
-
OracleStringList
public OracleStringList()Construct an empty oracle string list.
-
-
Method Details
-
size
public int size()Returns the number of items in this string list.- Specified by:
sizein interfaceStringList- Returns:
- the number of items in this string list
-
isEmpty
public boolean isEmpty()Returnstrueif this string list has no items. More formally, a string list is empty if, and only if, itssizeis zero.- Specified by:
isEmptyin interfaceStringList- Returns:
trueif this string list is empty;falseotherwise
-
add
Inserts an item into this string list at the specified index position. If an item was already at that position, then that item and subsequent items are shifted to the right (i.e., one is added to their indices).- Specified by:
addin interfaceStringList- Parameters:
index- index at which the specified string is to be inserteditem- item to be inserted- Returns:
trueif this list changed as a result of the call
-
add
Inserts multiple items into this string list at the specified index position. The relative order of inserted items is preserved. If items were already at that or subsequent positions, then those items are shifted to the right (i.e.,itemList.size()is added to their indices).Instead of inserting items into a string list one at a time, this method enables users to insert multiple items into a string list all at once. Here are some examples:
// first list StringList list1 = new OracleStringList(); list1.add(0, "a"); list1.add(1, "b"); // second list StringList list2 = new OracleStringList(); list2.add(0, "0"); list2.add(1, "1"); // insert second list into first list list1.add(1, list2); System.out.println(list1); // [a, 0, 1, b] // insert modified first list into itself list1.add(1, list1); System.out.println(list1); // [a, a, 0, 1, b, 0, 1, b]
The last code snippet shown above is an example of self-reference; that is, it is a scenario where the source and destination objects are the very same object. This is allowed! Implementers should take special care to avoid accidental infinite loops in situations involving self reference since thesizeof the source string list may change as items are inserted into the destination string list.- Specified by:
addin interfaceStringList- Parameters:
index- index at which the specified items are to be inserteditemList- string list of items to be inserted- Returns:
trueif this list changed as a result of the call
-
get
Returns the item at the specified index position in this string list.- Specified by:
getin interfaceStringList- Parameters:
index- index of the item to return- Returns:
- the item at the specified position in this string list
-
remove
Removes the item at the specified index position in this string list. Any items in the string list that were after the removed string are shifted to the left (i.e., one is subtracted from their indices).- Specified by:
removein interfaceStringList- Parameters:
index- index of the item to remove- Returns:
- the string that was removed
-
makeString
Returns a string representation of this string list that begins withstartand ends withend, with every string in the string list separated bysep. Here is an example, assuminglistrefers to an emptyStringList:String result1 = list.makeString("~", "!", "#"); String result2 = list.makeString(null, null, null); list.add(0, "a"); list.add(1, "b"); list.add(2, "c"); String result3 = list.makeString("~", "!", "#"); String result4 = list.makeString(null, null, null); System.out.println(result1); System.out.println(result2); System.out.println(result3); System.out.println(result4);The output would be:~# nullnull ~a!b!c# nullanullbnullcnull
- Specified by:
makeStringin interfaceStringList- Parameters:
start- specified starting stringsep- the specified separator stringend- the specified ending string- Returns:
- a string representation of this string list that begins with
startand ends withend, with every string in the string list separated bysep
-
toString
ReturnsmakeString("[", ", ", "]").- Specified by:
toStringin interfaceStringList- Overrides:
toStringin classObject- Returns:
makeString("[", ", ", "]")
-
clear
public void clear()Removes all of the items from this string list. The string list will beemptyafter this method returns.- Specified by:
clearin interfaceStringList
-
slice
Returns a new string list that contains the items from this list between the specifiedstartindex (inclusive) andstopindex (exclusive). This method does not modify the calling object. Ifstartandstopare in bounds and equal, then the returned string list is empty. The returned string list must be an object of the same class as the calling object. Here is an example, assuminglistrefers to a non-emptystring list with items referring to"a","b","c", and"d":System.out.println(list); // [a, b, c, d] StringList slice0 = list.slice(0, 4); System.out.println(slice0); // [a, b, c, d] System.out.println(list); // [a, b, c, d] StringList slice1 = list.slice(1, 3); System.out.println(slice1); // [b, c] System.out.println(list); // [a, b, c, d] StringList slice2 = list.slice(2, 4); System.out.println(slice2); // [c, d] System.out.println(list); // [a, b, c, d] StringList slice3 = list.slice(1, 1); System.out.println(slice3); // [] System.out.println(list); // [a, b, c, d]
- Specified by:
slicein interfaceStringList- Parameters:
start- left endpoint (inclusive) of the slicestop- right endpoint (exclusive) of the slice- Returns:
- a new string list with the items from this list from
start(inclusive) tostop(exclusive)
-
reverse
Returns a new string list that contains the items from this string list in reverse order. The returned string list must be an object of the same class as the calling object.- Specified by:
reversein interfaceStringList- Returns:
- a new string list with items from this list in reverse order
-
contains
Returnstrueifstart >= 0and there exists an item at or after thestartindex that equals thetargetstring. If no such item exists, thenfalseis returned. Unlike theaddmethods that throws exceptions when called with bad arguments, thiscontainsmethod simply returnsfalseinstead.- Specified by:
containsin interfaceStringList- Parameters:
start- the index from which to start the searchtarget- the item to search for- Returns:
trueif there exists anitemat or afterstartsuch thatitem.equals(target), orfalseif there is no such occurrence
-