public class StringSet {

    public static void main(String[] args){
        StringSet ss1 = new StringSet();
     * 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;
4 Years
Discussion Span
Last Post by jalpesh_007

Please 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 your program? And what the loop in lines 31~33 is for? While you are copying from old to new array (and extend the array size), you should have checked whether or not the data already exists. If it exists, ignore line 44.

PS: Where is your toString() method? You are trying to display the class instance (using System.out.println()) but you didn't provide toString() to show how you display the class instance information.


in main method, you have printed object information.
insted you have to print something else,because you can't get what is information stored in class object.

so modify it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.