1

我有这段代码可以搜索数组中的一个对象并将其删除。我的位置有问题,因为其他一些方法可以使用这个数组(它每次都会给我一个 NullPointerException)。我的方法如下所示:

public void deleteHotel(String hotelName) {
    for (int i = 0; i < this.hoteis.length; i++) {
        if (this.hoteis[i].getName().equalsIgnoreCase(nomeHotel)) { //searches the array, looking for the object that has the inputted name
            this.hoteis[i] = null; //makes that object null
            if (this.hoteis.length > 1 && this.hoteis[this.hoteis.length - 1] != null) { //for arrays with lenghts bigger than 1 (since there's no problem with an array with one position)
                for (int x = i; x < this.hoteis.length; x++) {
                    this.hoteis[x] = this.hoteis[x + 1]; //makes that null position point to the next position that has an object, and then that position points to the object in the next position and so on
                }
                this.hoteis[this.hoteis.length - 1] = null; //since the last to positions will be the same, make that last one null
                Hotel[] hoteisTemp = new Hotel[this.hoteis.length - 1];
                for(int x = 0; x < this.hoteis.length - 1; x++){ //create a new array with one less position, and then copy the objects on the old array into the new array, then point the old array to the new array
                    hoteisTemp[x] = this.hoteis[x];
                }
                this.hoteis = hoteisTemp;
            }
            i = this.hoteis.length;
        }
    }

}

当我使用其他方法(例如,返回每个对象的已实现 toString()s 的方法)时,它会给我一个 NullPointerException。你们能找出代码中的错误吗?非常感激...

4

5 回答 5

1

根本问题是您不允许从数组中删除条目的位置。

代替

for(int x = 0; x < this.hoteis.length - 1; x++){

你要

for(int x = 0; x < this.hoteisTemp.length; x++){

(虽然这是一种风格选择)

更重要的是,而不是

hoteisTemp[x] = this.hoteis[x];

你要

int y = x < i ? x : x + 1;
hoteisTemp[x] = this.hoteis[y];

您还希望摆脱将数组元素设置为的任何地方null,因为如果您的复制逻辑正常工作,那是不必要的。

对于这个用例,我会考虑使用其中一种List实现。

于 2013-07-17T21:14:58.740 回答
1

考虑重写你的代码

List result = new ArrayList();
for (int i = 0; i < this.hoteis.length; i++) {
    if (!this.hoteis[i].getName().equalsIgnoreCase(nomeHotel)) {
        result.add(this.hoteis[i]);
    }
}
return result.toArray();
于 2013-07-17T21:18:35.993 回答
1

我已经测试了你的函数,我明白你的意思是它得到一个空指针异常,这是由于数组没有调整列表的大小 - 这是由于你的条件:
if (this.hoteis.length > 1 && this.hoteis[this.hoteis.length - 1] != null)
简单地删除它解决了这个问题,这里是工作功能:

public static void deleteHotel(String hotelName) {
    for (int i = 0; i < hotels.length; i++) {
        if (hotels[i].getName().equalsIgnoreCase(hotelName)) { //searches the array, looking for the object that has the inputted name
            hotels[i] = null; //makes that object null
            for (int x = i; x < hotels.length -1; x++) 
                hotels[x] = hotels[x + 1]; //makes that null position point to the next position that has an object, and then that position points to the object in the next position and so on

            Hotel[] hoteisTemp = new Hotel[hotels.length - 1];
            for(int x = 0; x < hotels.length - 1; x++) //create a new array with one less position, and then copy the objects on the old array into the new array, then point the old array to the new array
                hoteisTemp[x] = hotels[x];

            hotels = hoteisTemp;
            break;
        }
    }
}

尽管在需要使用大小变化的列表时,请考虑使用某种列表。

于 2013-07-17T21:32:44.723 回答
0

将数组元素向左移动的点

for (int x = i; x < this.hoteis.length; x++) {
    this.hoteis[x] = this.hoteis[x + 1];
}

循环条件应该是x < this.hoteis.length - 1因为在最后一次迭代x = this.hoteis.length - 1中索引值this.hoteis[x + 1]会抛出一个NullPointerException.

于 2013-07-17T21:19:22.773 回答
0

尝试使用 ArrayList 它将简化您的代码复杂性。这里是文档的链接。 http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

于 2013-07-17T21:47:46.313 回答