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

标签 java arrays constructor interface copy-constructor

所以我目前正在从事一个项目,该项目正在为数组字符串列表和链接字符串列表重新创建方法。有一个 StringList 接口(interface),ArrayStringList 和 LinkedStringList 都实现了。我们不允许查看接口(interface)的源代码——只能查看 API 文档。对于每个类,我们必须为这两个类创建默认构造函数和复制构造函数。我已经运行了测试,默认构造函数都通过了,但是 ArrayStringList 复制构造函数不起作用并且一直在抛出“null”或“-1”的错误消息。我对继承和接口(interface)还很陌生,我认为对象参数与​​字符串数组数据类型让我有些困惑。

这是我目前的代码,以及构造函数中使用的方法:

我的复制构造函数:

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

尺寸方法:

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

获取方法:

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

设置方法:

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

在项目中,对拷贝构造函数的描述如下:

实现类必须显式定义复制构造函数。复制构造函数应该只接受接口(interface)类型 StringList 的一个参数。它应该使新构造的列表对象成为构造函数参数引用的列表的深拷贝。因此,新列表对象的初始大小和字符串元素将与另一个列表相同。需要明确的是,另一个列表可以是 StringList 接口(interface)的任何实现的对象。不应对对象的类型做出其他假设。

最佳答案

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 ...
}

请记住,此实现仍然存在问题,但您可以将其用作起点

关于java - Java中字符串数组的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58172776/

相关文章:

PHP - 按键长度对哈希数组进行排序

python - python 中类型构造函数的奇怪之处

javascript - JS中原始构造函数的用处

java - CompareTo 的通用比较类型

java - 迁移到 Spring 3 后从 Eclipse IDE 启动基于 Maven 的 GWT 应用程序时出现 Spring NamespaceHandler 问题

JavaScript 数组包含值之一

c - 在 C 中创建图形的替代方法

java - 有什么办法可以在 Thymeleaf 3.0.5 中添加 ExclusionStrategy 吗?

java - 为什么 Volley ImageRequest 返回位图,而不是其他图像文件格式?

c++ - 如何在 C++11 中正确初始化数据成员?