为什么ArrayList类的removeRange方法不起作用? [英] Why removeRange method of ArrayList class is not working?
问题描述
我正在尝试使用removeRange
方法从ArrayList
中删除某些元素.我从这里了解了这种方法: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#removeRange(int,int)
I am trying to use removeRange
method for removing certain elements from ArrayList
. I got to know about this method from here: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#removeRange(int, int)
但是当我这样尝试
ArrayList<String> al = new ArrayList<String>();
al.add("AB");
al.add("BC");
al.add("CD");
al.add("DE");
al.removeRange(1, 3);
我收到此错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method removeRange(int, int) from the type ArrayList<String> is not visible
为什么我不能使用这种方法?我在做错什么吗?
Why am I not able to use this method? Am I doing something wrong?
推荐答案
由于它是一种受保护的方法,因此仅对类,包和子类可见.
Since it a protected method , it is visible to only class ,package and subclasses.
protected修饰符指定成员只能在其自己的程序包中访问(与package-private一样),并且只能由其在另一个程序包中的类的子类访问.
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
Modifier Class Package Subclass World
---------------------------------------------
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol .html
这篇关于为什么ArrayList类的removeRange方法不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!