简体   繁体   English

Java 中的字符串数组的复制构造函数

[英]Copy Constructor for a String Array in Java

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. ArrayStringList 和 LinkedStringList 都有一个 StringList 接口。 We are not allowed to see the source code for the interface - only the API documentation.我们不允许查看接口的源代码 - 只有 API 文档。 For each class, we have to create a default constructor and copy constructor for both classes.对于每个 class,我们必须为这两个类创建一个默认构造函数和复制构造函数。 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".我已经运行了测试,默认构造函数都通过了,但是 ArrayStringList 复制构造函数不起作用,并且一直抛出“null”或“-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.我对 inheritance 和接口很陌生,我认为 object 参数与字符串数组数据类型让我有点失望。

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.实现 class 必须显式定义复制构造函数。 The copy constructor should take exactly one parameter of the interface type StringList.复制构造函数应该只采用接口类型 StringList 的一个参数。 It should make the newly constructed list object a deep copy of the list referred to by the constructor's parameter.它应该使新构造的列表 object 成为构造函数参数引用的列表的深层副本。 Therefore, the initial size and string elements of the new list object will be the same as the other list.因此,新列表 object 的初始大小和字符串元素将与其他列表相同。 To be clear, the other list can be an object of any implementation of the StringList interface.需要明确的是,另一个列表可以是 StringList 接口的任何实现的 object。 No other assumptions about the type of the object should be made.不应对 object 的类型做出其他假设。

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请记住,此实现仍然存在错误,但您可以将其用作起点

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我会将内部数组初始化为size的值,并利用String class 也有一个复制构造函数的事实

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));
    } 
}
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.并在 class object 的帮助下调用它。 it will set value in global static list.它将在全局 static 列表中设置值。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM