我正在实现我的自定义ArrayList类的整数与数组的帮助下,我想能够从我的数组中删除一个特定的值。我的问题是,当有多个相同的可删除值彼此相邻时,我会得到两个相邻的0,从而导致错误。我试图解决它几个小时没有运气。这里是我的代码:如何实现自定义ArrayList的deleteValues(int values)方法?
int max=10;
public int[] a = new int[max];
@Override
public void deleteValues(int value) {
int tempIndex=0;
for (int i = 0; i <50 ; i++) {
if (a[tempIndex] == value) {
a[tempIndex] = a[tempIndex + 1];
a[tempIndex + 1] = 0;
} else if (a[tempIndex] == 0) {
a[tempIndex] = a[tempIndex + 1];
a[tempIndex + 1] = 0;
} else {
tempIndex++;
}
}
}
我的阵列看起来像删除值(4)前:
[4, 2, 3, 4, 4, 4, 4, 1, 2, 3]
这是运行代码后的错误的结果:
[2, 3, 0, 0, 4, 4, 4, 1, 2, 3]
什么我想实现:[2, 3, 1, 2, 3, 0, 0, 0, 0, 0]
我的问题是:什么是最好的方法来ma关注代码工作,尽可能少地使用循环?
哪里'ArrayList'?什么是'a'? – Maroun
对不起,a是一个数组,我尝试实现自己的Arraylist类。 – codeme
你想从数组中删除元素还是将其设置为0? – Slavik