import java.util.ArrayList;public class RemoveTest { public class MyArrayList<E> extends ArrayList<E> { public void removeAfter(int index) { rangeCheck(index); super.removeRange(index + 1, size() - 1); } public MyArrayList() { } } public static void main(String args[]) { MyArrayList<Integer> List = new ArrayList<Integer>(); for (int i = 0; i < 10; i++) List.add(i); List.removeAfter(5); for (int i = 0; i < List.size(); i++) { System.out.println(List.get(i)); } } } ldh911,这段代码有问题哎。帮我改一下?十分感谢!
如下: public static void main(String[] args) { MyArrayList<Integer> lst = new MyArrayList<Integer>(); // 必须new你自己的实现类 for (int i = 0; i < 10; i++) lst.add(i); lst.removeAfter(5); for (int i = 0; i < lst.size(); i++) { System.out.println(lst.get(i)); } } public static class MyArrayList<E> extends ArrayList<E> { // 这个要加 static,因为你是静态方法中引用的。 public void removeAfter(int index) { // super.RangeCheck(index); // 这个是private的,没法被调用 if (index < 0 || index >= size()) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size()); } super.removeRange(index + 1, size()); } }
ldh911,如果想把这个静态内部类改成非静态内部类,那代码应该怎么写呢?非常感谢!public /*static*/ class MyArrayList<E> extends ArrayList<E> {
非静态的这样改就好 import java.util.ArrayList;public class RemoveTest { public class MyArrayList<E> extends ArrayList<E> {
public void removeAfter(int index) { if (index < 0 || index >= size()) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size()); } super.removeRange(index + 1, size()); } } public void remove() { MyArrayList<Integer> List = new MyArrayList<Integer>(); for (int i = 0; i < 10; i++) List.add(i); List.removeAfter(5); for (int i = 0; i < List.size(); i++) { System.out.println(List.get(i)); } } public static void main(String args[]) { new RemoveTest().remove(); } }
只能把 MyArrayList<Integer> List = new MyArrayList<Integer>(); 这个语句写在 main函数体外面么?
super.removeRange(index+1, size()-1);
rangeCheck(index);
super.removeRange(index+1, size()-1);
}
ldh911, 那这个函数放在什么地方?直接放在 ArrayList这个类里面??
public MyArrayList extends ArrayList {
public void removeafter(int index) {
rangeCheck(index);
super.removeRange(index+1, size()-1);
}
}
ArrayList.subList(0, index)删除后面的,意思不就是获取前面的。
class MyArrayList<T> extends ArrayList<T>
{
public void removeafter(int index)
{
if(index>=this.size()) //给出的索引超出范围.
{
return;
}
while(this.size()>index)
{
super.remove(index);
}
}
}
public class MyArrayList<E> extends ArrayList<E> {
public void removeAfter(int index) {
rangeCheck(index);
super.removeRange(index + 1, size() - 1);
} public MyArrayList() {
}
} public static void main(String args[]) {
MyArrayList<Integer> List = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
List.add(i); List.removeAfter(5);
for (int i = 0; i < List.size(); i++) {
System.out.println(List.get(i));
}
}
}
ldh911,这段代码有问题哎。帮我改一下?十分感谢!
MyArrayList<Integer> lst = new MyArrayList<Integer>(); // 必须new你自己的实现类
for (int i = 0; i < 10; i++) lst.add(i);
lst.removeAfter(5);
for (int i = 0; i < lst.size(); i++) {
System.out.println(lst.get(i));
}
} public static class MyArrayList<E> extends ArrayList<E> { // 这个要加 static,因为你是静态方法中引用的。
public void removeAfter(int index) {
// super.RangeCheck(index); // 这个是private的,没法被调用
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
}
super.removeRange(index + 1, size());
}
}
removeRange
protected void removeRange(int fromIndex,
int toIndex)移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。向左移动所有后续元素(减小其索引)。此调用将列表缩短了 (toIndex - fromIndex) 个元素。(如果 toIndex==fromIndex,则此操作无效。) 覆盖:
类 AbstractList<E> 中的 removeRange
参数:
fromIndex - 要移除的首个元素的索引
toIndex - 最后一个要移除的元素后面那个元素的索引
抛出:
IndexOutOfBoundsException - 如果 fromIndex 或 toIndex 超出范围 (fromIndex < 0 || fromIndex >= size() || toIndex > size() || toIndex < fromIndex)
哦,好的。你能顺便给个 API文档的下载链接么?还有如果想把这个静态内部类改成非静态内部类,那代码应该怎么写呢?
import java.util.ArrayList;public class RemoveTest {
public class MyArrayList<E> extends ArrayList<E> {
public void removeAfter(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
}
super.removeRange(index + 1, size());
}
} public void remove() {
MyArrayList<Integer> List = new MyArrayList<Integer>();
for (int i = 0; i < 10; i++)
List.add(i); List.removeAfter(5);
for (int i = 0; i < List.size(); i++) {
System.out.println(List.get(i));
}
} public static void main(String args[]) {
new RemoveTest().remove();
}
}
这个语句写在 main函数体外面么?