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
Recommended Answers
Jump to PostPlease provide full information of why it is null! You are expecting other people to assume what happens when you run the code...
Anyway, did you read the comment above the insert() method? You didn't follow the way it should. If the incoming argument is null, what would happen to …
All 2 Replies
Reply to this topic
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.