顺序表中的元素移动
2011-04-17 16:29
127 查看
// 移去index位置的对象,若操作成功,则返回被移去对象,否则返回null
public E remove(int index) {
if (this.n != 0 && index >= 0 && index < this.n) {
E old = (E) this.table[index];
for (int j = index; j < this.n - 1; j++) { // 元素前移,平均移动n/2
this.table[j] = this.table[j + 1];
}
// System.out.println(this.table[this.n - 1]);
this.table[this.n - 1] = null;
this.n--;
return old; // 若操作成功,则返回被移去对象
}
return null; // 未找到删除对象,操作不成功,,返回null
}
我认为,这里的元素移动实际上是复制加覆盖的,所以要加上这个this.table[this.n - 1] = null;
,否则的话this.table[this.n - 1] 还是原来的值,(在this.n--语句执行之前)现在就与this.table[this.n - 2] 的值相等了,
只不过最后没有显示罢了
public E remove(int index) {
if (this.n != 0 && index >= 0 && index < this.n) {
E old = (E) this.table[index];
for (int j = index; j < this.n - 1; j++) { // 元素前移,平均移动n/2
this.table[j] = this.table[j + 1];
}
// System.out.println(this.table[this.n - 1]);
this.table[this.n - 1] = null;
this.n--;
return old; // 若操作成功,则返回被移去对象
}
return null; // 未找到删除对象,操作不成功,,返回null
}
我认为,这里的元素移动实际上是复制加覆盖的,所以要加上这个this.table[this.n - 1] = null;
,否则的话this.table[this.n - 1] 还是原来的值,(在this.n--语句执行之前)现在就与this.table[this.n - 2] 的值相等了,
只不过最后没有显示罢了
相关文章推荐
- 两个select之间的元素互相移动并保持顺序
- 顺序表中元素的移动
- 已知一个几乎有序的数组,几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离可以不超过k,并且k相对于数组来说比较小。请选择一个合适的排序算法针对这个数据进行排序。 给定一个int数组A,同时给定
- 给定一个数组nums,写一个函数将所有的值移动0到最后,同时保持非零元素的相对顺序不变
- 如何打乱List 或 Array元素的顺序
- jquery实现tr元素的上下移动示例代码
- JavaScript基础 使用new关键字和Array()来创建空数组 并 不按顺序来添加元素
- js闭包判断点击的元素顺序
- 数组正负元素前后移动算法的实现(以0为分界线)
- python+selenium移动到二级元素按钮上和点击二级元素上的按钮
- 如何将一个数组的元素顺序打乱
- JS动态生成的元素,其对应的方法不响应(比如单击事件,鼠标移动事件等)
- Bootstrap3 移动设备优先并使用了一些HTML5元素
- jquery实现的元素的left增加N像素 鼠标移开会慢慢的移动到原来的位置
- 顺序表应用4:元素位置互换之逆置算法
- [SDUT](3324)顺序表应用1:多余元素删除之移位算法 ---顺序存储(线性表)
- JavaScript 对两个select 元素进行操作,移动其中的选项
- C++ 顺序容器的操作(6) 删除元素 erase、clear、pop_back、pop_front
- 华为机试题:有10个整数,使前面格数顺序向后移m个位置,最后m个数变成最前面m个数。计算移动后的整数序列的前m个数和后m个数的和。
- 顺序表应用4:元素位置互换之逆置算法