我有这个代码搜索数组中的一个对象并将其删除。我的位置有问题,因为其他一些方法可以处理这个数组(每次都会给我一个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()的方法)时,它会给我一个NullPointerException。你们可以识别代码中的错误吗?非常感谢...
答案 0 :(得分: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
实现。
答案 1 :(得分: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();
答案 2 :(得分:1)
我已经测试了你的功能,我看到你得到nullpointerexception的意思,这是由于数组没有调整列表的大小 - 这是由于你的条件:
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;
}
}
}
虽然在需要使用大小不断变化的列表时,请考虑使用某种列表。
答案 3 :(得分: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
。
答案 4 :(得分:0)
尝试使用ArrayList,它将简化您的代码复杂性。这是文档的链接。 http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html