問題は、 main メソッドに示されている文を生成するためにget(int index)、 、set(intindex)、およびコンストラクターの 3 つのメソッドを実装することです。SelfGrowingArray()コンパイル中にArrayIndexOutofBoundsException、具体的には次のエラーが発生します。
[null, null, null, null, null, null, null, null, null, null, null, null, null, null, You don't know you're beautiful]
[null, null, null, null, null, null, null, null, null, null, null, null, null, null, You don't know you're beautiful, null, null, null, null, null, null, What doesn't kill you makes you stronger.]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -14
これが私のコードです、助けてください:
public class SelfGrowingArray {
private Object[] data;
public SelfGrowingArray()
{
    data = new Object[0];
}
public void set(int index, Object value)
{
    if (index >= data.length) {
        Object[] newArray = new Object[index + 1];
        for (int i = 0; i < data.length; i++) {
        newArray[i] = data[i];
        }
        data = newArray;
        }
        data[index] = value;    
}
public Object get(int index)
{
    if (index >= data.length)
    return null;
    return data[index];
}
public String toString()
{
    if (data == null)
    {
        return "null";
    }
    int iMax = data.length - 1;
    if (iMax == -1)
    {
        return "[]";
    }
    StringBuilder b = new StringBuilder();
    b.append('[');
    for (int i = 0; ; i++)
    {
        b.append(data[i]);
        if (i == iMax)
        {
            return b.append(']').toString();
        }
        b.append(", ");
    }
}
public static void main(String[] args) {
    SelfGrowingArray myList = new SelfGrowingArray();
    myList.set(14, "You don't know you're beautiful");
    System.out.println(myList);
    myList.set(21, "What doesn't kill you makes you stronger.");
    System.out.println(myList);
    System.out.println("myList.get(-14) " + myList.get(-14));
    System.out.println("myList.get(14) " + myList.get(14));
    System.out.println("myList.get(15) " + myList.get(15));
    System.out.println("myList.get(31) " + myList.get(31));
    System.out.println("myList.get(32) " + myList.get(32));
}
}