作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Car
的 Array
,其中包含 Car
对象,并且我已经在获取的方法上停留了一段时间了一个汽车号码,需要删除所有具有该汽车号码的对象,然后返回该数组,没有任何漏洞。这是我的作业,所以我不能使用任何 arrayList
或 Array.
方法。
我尝试执行以下操作,但由于某种原因,当我尝试运行它时,它不符合要求,并给我一个 IndexOutOfBounds
错误,并请我引用我的 toString
方法(它打印数组中的所有单元格)。
noOfCars
,保存当前实际的汽车数量,因为我们使用容器构造函数构建了数组,以便为 future 的汽车保留更多位置,以防万一。
public void removeCarNumbers(int CarNum) {
int count = 0;
for(int i = 0; i < this.noOfCars; i++) {
if(this.cars[i].getCarNum() == CarNum) {
this.cars[i] = this.cars[noOfCars- 1 - count];
count++;
}
}
CarsLines [] newArr = new CarLines[this.noOfCars- count];
for(int i = 0; i< this.noOfCars - count; i++)
{
newArr[i] = this.cars[i];
}
this.cars= newArr;
}
*编辑:在玩了更多之后,我仍然收到一个错误,将我引向我的 toString() 方法。它也有问题吗? 公共(public)字符串 toString() { 字符串输出=“”; for (int i = 0; i < this.noOfCars; i++) 输出 += ""+ this.cars[i].toString() + "\n"; 返回输出; }
最佳答案
尝试:
// count
int count = 0;
for(int i = 0 ; i < this.cars.length ; ++i) {
if(this.cars[i].getCarNum() == CarNum) {
++count;
}
}
// create new array
CarsLines[] newArr = new CarLines[this.cars.length - count];
int index = 0;
for(int i = 0 ; i < this.cars.length ; ++i) {
if(this.cars[i].getCarNum() != CarNum) {
newArr[index++] = this.cars[i];
}
}
// assign
this.cars = newArr;
关于java - 从无孔阵列中移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23474159/
我有两个多边形。我想将它们组合成一个多边形,使其仅包含外部点而没有任何孔。我怎样才能做到这一点?很少解释的代码将非常有帮助。谢谢。 请参阅图片以获得更好的理解。 最佳答案 我认为您可以只使用union
我是一名优秀的程序员,十分优秀!