Algorithms and Data Structures in
an Object-Oriented Framework (“ADSOOF”)
Code for “Java's built-in classes” section
Code demonstrating Strings
- ScannerTest1.java
Demonstrates using a
Scanner
object to break a line of text
into individual words.
- ScannerTest2.java
Demonstrates using a
Scanner
object to break text
from a file into individual lines.
- StringTest1.java
Shows and demonstrates a static method which takes a string and two
characters and returns the string given by replacing all occurrences of the
first character in the string by the second character.
- StringTest2.java
Demonstrates the same operation as in StringTest1.java, but done using one
of Java's built-in non-static String methods (so the method is called on
a string rather than the string passed as an argument to it).
- StringTest3.java
Demonstrates using the built-in string method
substring
.
- StringTest4.java
Demonstrates using the built-in string methods
startsWith
,
endsWith
and compareTo
.
- StringTest5.java
Demonstrates using the built-in string methods
indexOf
and lastIndexOf
.
- StringTest6.java
Demonstrates using the built-in string methods
toUpperCase
and toLowerCase
.
- StringTest7.java
Demonstrates converting arrays to strings and vice-versa.
- StringTest8.java
Demonstrates using the method
toBinaryString
from wrapper
class Integer
.
- StringTest9.java
Demonstrates using some methods from wrapper class
Character
.
- StringTest10.java
Shows and demonstrates the implementation of a method which performs the
startsWith
operation as in StringTest4.java, but as
a static operation, so both strings are arguments. Method is implemented
using iteration.
- StringTest11.java
Shows and demonstrates a method which perfoms the same operation as in
StringTest10.java, but is implemented using recursion rather than iteration.
- StringTest12.java
Shows and demonstrates a method which perfoms the same operation as in
StringTest10.java, with the tail recursion of StringTest11.java
converted into iteration.
- StringTest13.java
Shows and demonstrates a method which performs the same operation as in
StringTest1.java, but is implemented using recursion and not iteration.
- StringTest14.java
Shows the difference between
==
and equals
with strings.
Code demonstrating ArrayLists
- UseArrayLists1.java
Demonstrates a static method which takes an arrayList of integers as an argument
and two integers and changes all occurrences of the first integer to the second
destructively. Numbers entered into the arrayList as if it were an array.
- UseArrayList2.java
Demonstrates a static method which takes an arrayList of integers and returns
a copy, using the
add
method from class ArrayList
.
- UseArrayList2a.java
Demonstrates using
clone
to make a copy of an
arrayList.
- UseArrayList3.java
Demonstrates making a copy of an arrayList of integers using the constructor which
takes an arrayList argument from class
ArrayList
.
- UseArrayLists4.java
Demonstrates a static method which takes an arrayList of integers and two
integers as arguments and inserts the second integer after all occurrences
of the first integer, doing it destructively.
- UseArrayLists5.java
Demonstrates a version of the static method in UseArrayLists4.java which
does not go into an infinite loop when both argument integers are the same.
- UseArrayLists6.java
Demonstrates a static method which takes an arrayList of integers and two
integers as arguments and inserts the second integer after all occurrences of
the first integer, doing it constructively (that is, returns a new arrayList
with the required changes).
- UseArrayLists7.java
Demonstrates a version of the static method in UseArrayLists6.java which
avoids unnecessary repeated calls of the method
get
.
- UseArrayLists8.java
Demonstrates a static method which takes an arrayList of strings and two
strings as arguments and inserts the second string after all occurrences of
the first string, doing it constructively. So, same operation as
UseArrayLists7.java
, but deals with strings rather than integers.
- UseArrayLists9.java
Demonstrates the use of two static methods which take an arrayList
and two objects of its base type, and insert the second object after all
occurrences of the first object constructively. In one case, the base type
is integers, in the other strings.
- UseArrayLists10.java
Demonstrates the use of a single static methods which take an arrayList
and two objects of its base type, where the static method has a type
variable to give the base type. The method is called twice, the
first time setting the type variable to
Integer
, the
second time setting it to String
.
- UseArrayLists11.java
Demonstrates calling various static methods which take arrayLists as
arguments, taking them from a separate class of static methods.
- UseArrayLists12.java
Demonstrates removal of a particular integer from an
arrayList of integers and removal of an integer from a particular
position (the two different forms of the
remove
method).
- UseArrayLists13.java
Same as
UseArrayLists12.java
, but uses an arrayList of strings instead of
an arrayList of integers.
- UseArrayLists14.java
Demonstrates a static method which takes an arrayList of integers and an
integer as its arguments and removes destructively each integer which occurs
before each occurrence of the integer argument. It removes the integer
argument itself when it occurs before another occurrence of itself.
- UseArrayLists15.java
Same as
UseArrayLists14.java>
, but does the operation constructively
instead of destructively.
- ArrayListOps
A class of static methods which work on arrayLists and are generic
so they will work on arrayLists of any base type. There are methods to
reverse an arrayList, to take an arrayList and two objects and change
all occurrences of the first object in the arrayList to the second object,
and to take an arrayList and two objects and insert the second object
after all occurrences of the first. There are destructive and constructive
versions of each method.
Code demonstrating generics
- GenericArrayTest.java
Demonstrates a generic method to find whether an item is present in
an array of items of that type, with the method used for an array of
Strings and an array of Integers.
- UseArrayLists16.java
Demonstrates a generic static method which takes an arrayList of objects as
an argument and two objects of its element type and changes all occurrences of
the first object to the second destructively. To use this code you also need
the
.class
files:
- UseArrayLists17.java
Demonstrates the difference between comparing two object references using
equals
and using ==
Text files
Text files, to be used as data for exercise.
Matthew Huntbach
Last modified: 16 July 2019