public class StringSet {
public static void main(String[] args){
StringSet ss1 = new StringSet();
ss1.insert("the");
System.out.println(ss1);
}
/**
* Creates an empty StringSet object.
*/
private String[] data;
public StringSet () {
data = new String[0];
}
/**
* If e is null, throws an IllegalArgumentException.
* Else, if there is already an element in the StringSet that
* is equal (in the .equals sense) to e, does nothing.
* Otherwise, adds e to the StringSet.
*/
public void insert (String e) {
int i = data.length;
String[] newData = new String[data.length+1];
// Copy data[j] to newData[j], for 0 <= j < i
for (int j = 0; j < i; j++) {
newData[j] = data[j];
}
// Add the element
newData[i] = e;
// Copy data[j] to newData[j+1], for i <= j < data.length
for (int j = i; j < data.length; j++) {
newData[j+1] = data[j];
}
// Save the new array in the object
data = newData;
}
ctclements 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.