1

So I'm currently working on a project that is recreating methods for Array String Lists and Linked String Lists. There is a StringList interface, that both ArrayStringList and LinkedStringList implement. We are not allowed to see the source code for the interface - only the API documentation. For each class, we have to create a default constructor and copy constructor for both classes. I've ran tests, and the default constructors both pass but the ArrayStringList copy constructor does not work and has been throwing the error message of "null" or "-1". I am pretty new to inheritance and interfaces, and I think the object parameters vs string array data types are throwing me off a bit.

Here is the code I have so far, and the methods used in the constructor:

My Copy Constructor:

private String[] stringArray;
private int size;

public ArrayStringList(StringList sl) {
    size = sl.size();
    ArrayStringList asl = new ArrayStringList();
    for(int i = 0; i < size-1; i++) {
        if(sl.get(i) != null) {
            asl.set(i,sl.get(i).toString());
        } //if
    } // for
} // copy constructor

Size Method:

public int size() {
    return stringArray.length;
} // size

Get Method:

public String get(int index) {
    if(index < 0 || index >= size) {
        throw new IndexOutOfBoundsException("out of bounds");
} else {
        return stringArray[index];
    }
} //get

Set Method:

public String set(int index, String s) {
    String old = stringArray[index];
stringArray[index] = s;
    return old;
} // set

In the project, the description of the copy constructor was as follows:

The implementing class must explicitly define a copy constructor. The copy constructor should take exactly one parameter of the interface type StringList. It should make the newly constructed list object a deep copy of the list referred to by the constructor's parameter. Therefore, the initial size and string elements of the new list object will be the same as the other list. To be clear, the other list can be an object of any implementation of the StringList interface. No other assumptions about the type of the object should be made.

11
  • if(sl.get(i) != null) why are you checking null here? Commented Sep 30, 2019 at 18:00
  • post documentation of set method Commented Sep 30, 2019 at 18:01
  • can you define ArrayStringList<String> asl = new ArrayStringList<>(); like this ? Commented Sep 30, 2019 at 18:01
  • please, provide the Interface you are trying to implement Commented Sep 30, 2019 at 18:02
  • @SSP - I've added the set. And I just tried adding that in to try to get rid of the null error and see where exactly it was occurring. Commented Sep 30, 2019 at 18:03

3 Answers 3

2
public class ArrayStringList implements StringList {

  private static final int INITIAL_CAPACITY = 10;

  private String[] stringArray;
  private int size;

  public ArrayStringList(StringList sl) {
    stringArray = sl.toArray();
    size = stringArray.length;
  }


  public ArrayStringList() {
    stringArray = new String[INITIAL_CAPACITY];
    size = 0;
  }

  // TODO: Extract 'if-cascade' to an validate(..) method 
  @Override
  public String set(int index, String s) {
    if (index >= size) {
      throw new IndexOutOfBoundsException("")
    } else if (s == null) {
      throw new NullPointerException("the specified string is null");
    } else if (s.isEmpty()) {
      throw new IllegalArgumentException("specified string is empty");
    }
    String old = stringArray[index];
    stringArray[index] = s;
    return old;
  }

  // TODO: Check if posible to extend the stringArray
  @Override
  public boolean add(String s) {
    if (s == null) {
      throw new NullPointerException("the specified string is null");
    } else if (s.isEmpty()) {
      throw new IllegalArgumentException("specified string is empty");
    }

    if (size == stringArray.length) {
      int newListCapacity = stringArray.length * 2;
      stringArray = Arrays.copyOf(stringArray, newListCapacity);
    }
    stringArray[++size] = s;
    return true;
  }

  // TODO: implement other methods ...
}

Keep in mind that this implementation is still buggy, but you can use it as a starting point

Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome! PS: I've just updated the default constructor and add(..) method
1
public void ArrayStringList(StringList sl) {
    size = sl.size();
    ArrayStringList asl = new ArrayStringList();
    for(int i = 0; i < size; i++) {
        if(sl.get(i) != null) {
            String s  = asl.set(i,sl.get(i).toString());
            System.out.println(s);
        } //if
    } // for
}

Change set method like below. And call it by the help of class object. it will set value in global static list.

//Change set method like this
public String set(int index, String s) {

    stringArray[index] = s;
    return stringArray[index];
}

Comments

1

I would initialise the internal array to the value of size and also make use of the fact that the String class also has a copy-constructor

public ArrayStringList(StringList sl) {
    this.size = sl.size();
    this.stringArray = new String[size];
    for(int j = 0; j < size; j++) {
        this.stringArray[j] = new String(sl.get(i));
    } 
}

5 Comments

This is so helpful, thank you! It doesn't seem to compile on that last line, with String(sl.get(i)); and says it cannot find the symbol. Would I use StringList instead?
It does compile when I take out the String all together. Would that still work the same? When I run a test without the String, it still throws the -1 error.
@FrancesBrown The -1 error might be related to your iteration, which stops at i < size-1 - in case you did not copy the code from this answer.
Your String(sl.get(i)) is not valid Java. Additionally, the String class in Java is immutable and does not need to be copied.
@FrancesBrown sorry I missed the new keyword

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.