为什么 ArrayList 类的 removeRange 方法不起作用? [英] Why removeRange method of ArrayList class is not working?

查看:62
本文介绍了为什么 ArrayList 类的 removeRange 方法不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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 修饰符指定该成员只能在它自己的包内访问(如包私有),此外,它的类的子类在另一个包中可以访问.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆