java - 给定空列表,然后添加 String (ArrayList 实现)

标签 java arrays arraylist

我正在尝试实现我自己的ArrayList。 这是我到目前为止的代码:

public class StringArrayList {
    private int size = 0;
    private String[] strings = new String[0];

    // Constructors
    public StringArrayList(){
        strings = new String[size];
    };
    public StringArrayList(int initialCapacity){
        strings = new String[0];
    }
    public StringArrayList(String[] strings){
        this.strings = strings;
        size = strings.length;
    }

    public int size() {
        return size;
    }

    public String get(int index){
        return strings[index];
    }

    public boolean add(String content){
        boolean add = false;
        strings = new String[10];
        for(int i = 0; i < strings.length; i++){
            if(strings[i] == null) {
                strings[i] = content;
                return true;
            }
        }
        return add;
    }

我必须使以下测试成功:

     public void add__given_empty_list__then_adds_string()
    {
        StringArrayList lst = list();

        lst.add( "string" );

        assertSameItems( list( "string" ), lst );
    }

我收到此错误:

java.lang.AssertionError: expected<1> but was <0>

我实际上迷失了对我的要求。我知道 null 和空列表之间存在差异,但如果我不指定 null ,我将收到 ArrayOutOfBounds 异常。

我不明白什么。

谢谢

最佳答案

您的实现存在一些错误。

public StringArrayList(int initialCapacity){
    strings = new String[0];
}

这种类型的构造函数旨在分配初始容量,而不是“0”。

最重要的是,您的add方法实际上并不添加新元素,而是覆盖当前的内部数组,并将新元素放置在第 0 个索引处(不增加 size 变量)。

public boolean add(String content){
    boolean add = false; //unnecessary, can replace bottom `add` with `false` for same result
    strings = new String[10]; //overwrites internal array of "list" with a new array
    for(int i = 0; i < strings.length; i++){
        if(strings[i] == null) { //will always replace only first element as this is a new array
            strings[i] = content;
            return true;
        }
    }
    return add;
}

size变量旨在跟踪当前数组包含多少个元素(当添加新元素时,该元素会增加)。

知道了这一点,您就可以在 strings[size] 中添加新元素添加新元素,以防 size < strings.length是真的。

如果不满足该条件(您想要添加超过当前容量的内容),则应通过创建新数组,将所有先前元素复制到新数组中,然后替换旧数组来“调整”内部数组的大小,并将新元素添加到新数组中。

编辑:您的新代码

public boolean add(String content) { 
    strings = new String[strings.length+1]; //still erases the internal array!
    strings[0] = content; //only modifies the first element, rather than add to the list
    size = 1; //if it was implemented correctly, this would be size++;
    return true; 
} 

进行一个测试,将两个元素添加到列表中,并断言列表是否包含这两个元素。

编辑2:

这是一个应该有效的解决方案:

public class StringArrayList {
    private int size = 0;
    private String[] strings;

    // Constructors
    public StringArrayList() {
        this(0);
    };
    public StringArrayList(int initialCapacity){
        String[] innerStrings = new String[initialCapacity];
        this(innerStrings);
    }
    public StringArrayList(String[] strings){
        this.strings = strings;
    }

    public int size() {
        return size;
    }

    public String get(int index){
        return strings[index];
    }

    public boolean add(String content){
        if(size == strings.length) {
            String[] newStrings = new String[size+10];
            for(int i = 0; i < size; i++) {
                newStrings[i] = strings[i];
            }
            strings = newStrings;
        }
        strings[size++] = content;
        return true;
    }
}

编辑3:

public boolean add(String content){ 
    if(size == strings.length) { 
        String[] temp_list = new String[strings.length]; //no need to allocate a new array here if this is just to store your current array
        temp_list = strings; 
        strings = new String[size++]; //wrong, this allocates a `size`-long array and increases `size` by 1 afterwards, rather than create a new, larger internal array
        strings = temp_list; //this just overwrites your internal array with the old array which is not increased in size

     } 
     for(int i = 0; i < strings.length; i++) { 
         if(strings[i] == null) { //this is not necessary with proper `size`
             strings[i] = content; 
         } 
     } 
     return true; 
}

关于java - 给定空列表,然后添加 String (ArrayList 实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29651025/

相关文章:

java - HttpClient 多线程性能

JavaScript 对象数组未正确推送

java - JTable在arrayList中显示数据

java - Android 代码帮助从列表末尾删除

c - Int 到 char 数组。它是如何工作的?

java - 将 ArrayList 制作成 JTable

Java, HashMap 中的 HashMap

java - Java System.in/out/err 是否始终打开?

java - 一台机器可以使用Java中的socket同时充当客户端和服务器吗?

java - 混淆使用什么 - Tomcat 或 java 套接字服务器