【问题标题】:Rearranging array when it has a null position当数组具有空位置时重新排列数组
【发布时间】:2013-07-17 21:09:47
【问题描述】:

我有这段代码可以搜索数组中的一个对象并将其删除。我的位置有问题,因为其他一些方法可以使用这个数组(它每次都会给我一个 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。你们能找出代码中的错误吗?非常感谢...

【问题讨论】:

  • 当您使用ArrayList 时,您确实意识到此功能开箱即用,对吧?所有这些代码都简化为:list.remove("name")
  • 如果我没有,我不会问这个问题,我会寻找它。不过,我想用数组试试这个。
  • 不重新排列所有的数组,为什么不把数组的最后一个对象移动到已删除酒店的位置呢?然后你可以从头到尾进行处理 ||空指针。这将需要一个变量来存储最后一个对象的位置。或者它可以是一个简单的计数器。

标签: java arrays nullpointerexception


【解决方案1】:

我已经测试了你的函数,我明白你的意思是它得到一个空指针异常,这是由于数组没有调整列表的大小 - 这是由于你的条件:
if (this.hoteis.length &gt; 1 &amp;&amp; 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;
        }
    }
}

虽然在需要使用大小可变的列表时,请考虑使用某种列表。

【讨论】:

  • 啊,我明白了……非常感谢。我知道列表,但我正在尝试一种不同的方法。你们都非常有帮助,谢谢。
【解决方案2】:

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

代替

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 实现之一。

【讨论】:

    【解决方案3】:

    考虑重写你的代码

    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();
    

    【讨论】:

    • 这可能是最简单的方法。虽然实际上最好保留列表,而不是返回数组。
    【解决方案4】:

    将数组元素向左移动的点

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

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多