ArrayList类添加方法

时间:2016-03-19 08:30:00

标签: java arraylist

我们正在编写一个在指定位置添加条目的add方法。它应该在位置要求的任何地方添加它。如果位置在1和数组大小之间,则它执行该功能。在我们的书中,链表从1开始而不是0.不知道为什么但请注意。我的问题是什么?我在for循环中遇到了麻烦。我知道我的问题在其中,但我不知道自己做错了什么。它不会添加任何东西,我的测试都不起作用。确保容量使数组相应变大(如果它已满)

public ArrayList()
{
    this(DEFAULT_INITIAL_CAPACITY);
} // end default constructor

public ArrayList(int initialCapacity)
{
    mySize = 0;
    @SuppressWarnings("unchecked")       
    T[] tempArray = (T[]) new Object[initialCapacity];
    myArray = tempArray;
} // end constructor
private void ensureCapacity()
{
    if (mySize == myArray.length)
        myArray = Arrays.copyOf(myArray, 2 * myArray.length);
}


public boolean add(int newPosition, T newEntry)
{
    if (newPosition >= 1 && (newPosition <= mySize || newPosition <= DEFAULT_INITIAL_CAPACITY)) {
        ensureCapacity();
        int index = newPosition-1;
        for (int i = mySize; i > 0; i--) {
            myArray[i] = myArray[i-1];
            if (i == index)
            myArray[index] = newEntry;
            break;
        }
        return true;
    }
    return false;
} // end add

这是测试代码:

System.out.println("Testing clear() ");
    myList.clear();

    System.out.println("List should be empty: ");
    System.out.println("Is list empty? " + myList.isEmpty());

    System.out.println("-------------------------\n");
    System.out.println("Create a new list of max size 8.\n");
    myList = new ArrayList<String>(8);  // 8 is max size

    System.out.println("Testing add at position ");
    System.out.println("Add 15 at position 1: returns " + myList.add(1, "15"));
    System.out.println("Add 25 at position 2: returns " + myList.add(2, "25"));
    System.out.println("Add 35 at position 3: returns " + myList.add(3, "35"));
    System.out.println("Add 99 at position 0: returns " + myList.add(0, "99"));
    System.out.println("Add 99 at position 9: returns " + myList.add(9, "99"));

    System.out.println("\n\nList should contain\n15 25 35 ");
    displayList(myList);

    System.out.println("Is List empty? " + myList.isEmpty());

    System.out.println("Add 19 at position 1: returns " + myList.add(1, "19"));
    System.out.println("Add 39 at position 3: returns " + myList.add(3, "39"));
    System.out.println("Add 29 at position 2: returns " + myList.add(2, "29"));
    System.out.println("Add 55 at position 7: returns " + myList.add(myList.getLength()+1, "55"));
    System.out.println("Add 65 at position 8: returns " + myList.add(8, "65"));

    System.out.println("\n\nList should contain\n19 29 15 39 25 35 55 65");
    displayList(myList);

    System.out.println("Is List empty? " + myList.isEmpty());

3 个答案:

答案 0 :(得分:1)

确保在添加元素后增加mySize。

此外,在开始时,mySize为0,因此永远不会输入你的for循环。

答案 1 :(得分:1)

如果我必须在不使用System.arraycopy本机方法的情况下实现添加位置,我会像这样实现它: 您还可以从arraylist(包java.util);

中检查源代码

您需要限制长度而不是遍历到数组的末尾,可以使其停止在数组的当前大小。但我没有看到你目前在任何地方存储大小。

<?php
$start_date = '25-02-2016';
$number_of_dates = 10;
$dates = array();
for($i=1;$i<=$number_of_dates;$i++){
  $date = date('Y-m-d', strtotime("$start_date +$i days"));
  echo "\n".$date;
  $dates[] = $date;  
}
?>

答案 2 :(得分:1)

看起来你在for循环中的if语句周围缺少大括号,这导致它在第一次循环之后爆发:

public boolean add(int newPosition, T newEntry)
    {
        if (newPosition >= 1 && (newPosition <= mySize || newPosition <= DEFAULT_INITIAL_CAPACITY)) {
        ensureCapacity();
        int index = newPosition-1;
        for (int i = mySize; i > 0; i--) {
            myArray[i] = myArray[i-1];

            // The break here needs to be associated with the if
            if (i == index) {
                myArray[index] = newEntry;
                // As Ridcully mentioned, you should increment your size here.
                mySize++;
                break;
            }
        }
        return true;
    }
    return false;
} // end add