请问如何新定义 ArrayList方法?十分感谢!

blackkettle 2012-11-08 03:09:29
各位达人,

我在使用 ArrayList的时候,需要新定义一个方法,要求删除 ArrayList里某个指标之后的所有的元素。

public void removeafter(int index) {
rangeCheck(index);

...

}

请问这个方法应该怎么写?

非常感谢!
...全文
321 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackkettle 2012-11-09
  • 打赏
  • 举报
回复
引用 14 楼 zqlee808 的回复:
非静态的这样改就好 import java.util.ArrayList; public class RemoveTest { public class MyArrayList<E> extends ArrayList<E> { public void removeAfter(int index) { if (index < 0 || index >=……
只能把
MyArrayList<Integer> List = new MyArrayList<Integer>();
这个语句写在 main函数体外面么?
zqlee808 2012-11-09
  • 打赏
  • 举报
回复
非静态的这样改就好 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(); } }
blackkettle 2012-11-09
  • 打赏
  • 举报
回复
引用 12 楼 zhaoming262350 的回复:
你查一下API文档就行了: removeRange protected void removeRange(int fromIndex, int toIndex)移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。向左移动所有后续元素(减小其索引)。此调用将列表缩短了 (toIn……
哦,好的。你能顺便给个 API文档的下载链接么? 还有 如果想把这个静态内部类改成非静态内部类,那代码应该怎么写呢?
zhaoming262350 2012-11-09
  • 打赏
  • 举报
回复
你查一下API文档就行了: 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)
blackkettle 2012-11-09
  • 打赏
  • 举报
回复
ldh911,如果想把这个静态内部类改成非静态内部类,那代码应该怎么写呢? 非常感谢!
public /*static*/ class MyArrayList<E> extends ArrayList<E> { 
blackkettle 2012-11-09
  • 打赏
  • 举报
回复
引用 16 楼 zqlee808 的回复:
main方法是静态的 所以不能直接调用动态的内部类
哦,受教了!
zqlee808 2012-11-09
  • 打赏
  • 举报
回复
main方法是静态的 所以不能直接调用动态的内部类
blackkettle 2012-11-08
  • 打赏
  • 举报
回复
引用 9 楼 ldh911 的回复:
如下: Java code12345678910111213141516171819 public static void main(String[] args) { MyArrayList<Integer> lst = new MyArrayList<Integer>(); // 必须new你自己的实现类 for (int i = 0……
ldh911,太感谢了!
MiceRice 2012-11-08
  • 打赏
  • 举报
回复
如下:

    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());
        }
    }
blackkettle 2012-11-08
  • 打赏
  • 举报
回复
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,这段代码有问题哎。帮我改一下? 十分感谢!
nmyangym 2012-11-08
  • 打赏
  • 举报
回复

class MyArrayList<T> extends ArrayList<T>
{
	public void removeafter(int index)
	{
		if(index>=this.size())			//给出的索引超出范围.
		{
			return;
		}
		while(this.size()>index)
		{
			super.remove(index);
		}
	}
}
MiceRice 2012-11-08
  • 打赏
  • 举报
回复
引用 5 楼 faping_cao 的回复:
删除后面的,意思不就是获取前面的。
略微不太一样,前者一般来说是要修改对象自身,你的招数是生成一个新的对象。
faping_cao 2012-11-08
  • 打赏
  • 举报
回复

ArrayList.subList(0, index)
删除后面的,意思不就是获取前面的。
MiceRice 2012-11-08
  • 打赏
  • 举报
回复
咋可能直接让你去修改Java自带的类。。。 一般这样:
public MyArrayList extends ArrayList {
    public void removeafter(int index) {
        rangeCheck(index);
        super.removeRange(index+1, size()-1); 
    }
}
blackkettle 2012-11-08
  • 打赏
  • 举报
回复
ldh911,能直接给个例子么?简单的能运行的就好了
blackkettle 2012-11-08
  • 打赏
  • 举报
回复
public void removeafter(int index) { rangeCheck(index); super.removeRange(index+1, size()-1); } ldh911, 那这个函数放在什么地方?直接放在 ArrayList这个类里面??
MiceRice 2012-11-08
  • 打赏
  • 举报
回复
这样如何? super.removeRange(index+1, size()-1);

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧