Java 为什么 ArrayList 类的 removeRange 方法不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20748848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:04:36  来源:igfitidea点击:

Why removeRange method of ArrayList class is not working?

javaarraylist

提问by Chaitanya

I am trying to use removeRangemethod 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)

我正在尝试使用removeRange方法从ArrayList. 我从这里了解了这种方法:http: //docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#removeRange(int, int)

However when I tried it like this

但是当我像这样尝试时

ArrayList<String> al = new ArrayList<String>();
al.add("AB");
al.add("BC");
al.add("CD");
al.add("DE");
al.removeRange(1, 3);

I got this error:

我收到此错误:

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?

为什么我不能使用这种方法?难道我做错了什么?

采纳答案by Suresh Atta

Since it a protected method , it is visible to only class ,package and subclasses.

因为它是一个受保护的方法,所以它只对类、包和子类可见。

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.

protected 修饰符指定该成员只能在它自己的包内访问(如包私有),此外,它的类的子类在另一个包中可以访问。

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

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

回答by René Link

The short answer is: Use

简短的回答是:使用

al.subList(1, 3).clear();


The removeRange(int, int)method is protected. You can only invoke it from a subclass of ArrayListor from a class within the same package as ArrayList. See Controlling Access to Members of a Class.

removeRange(int, int)方法protected。您只能从ArrayListArrayList. 请参阅控制对类成员的访问

The only way to access the removeRangemethod is to subclass ArrayListand make the method public. E.g.

访问该removeRange方法的唯一方法是子类化ArrayList并公开该方法。例如

public class RangeRemoveSupport<E> extends ArrayList<E> {

  public void removeRange(int fromIndex, int toIndex) {
    super.removeRange(fromIndex, toIndex);
  }

}

But then your code must use the subclass. Thus your code depends on this subclass and not just depends on Listor ArrayList.

但是你的代码必须使用子类。因此,您的代码取决于此子类,而不仅仅是取决于Listor ArrayList

A utility class within the same package to access it is not possible. E.g.

同一个包中的实用程序类无法访问它。例如

package java.util; // <- SecurityException

public class RemoveRangeSupport {

    public static void removeRange(ArrayList<?> list, int from, int to){
       list.removeRange(from, to);
    }
}

This will cause a SecurityException

这将导致一个 SecurityException

java.lang.SecurityException: Prohibited package name: java.util.

java.lang.SecurityException: Prohibited package name: java.util.

because you are not allowed to define classes in java.utilfor security reasons.

因为java.util出于安全原因,您不允许定义类。

Nevertheless for other packages it might be a way.

不过对于其他包,这可能是一种方式。

I often use this strategy for tests. Then I put such a utility class in the same package as the production code to access some internals from tests that should normally not be accessible. This is an easy way without using a framework.

我经常使用这种策略进行测试。然后我将这样一个实用程序类与生产代码放在同一个包中,以便从通常无法访问的测试中访问一些内部组件。这是一种无需使用框架的简单方法。

EDIT

编辑

Is there perhaps a function to replace items from range X..Y, to new items of a possible different size?

for example: this list "0,1,2,3,4", I replace from 1..3 with "a,b,c,d,e", will result with : "0,a,b,c,d,e,4".

是否有一个功能可以将范围 X..Y 中的项目替换为可能不同大小的新项目?

例如:这个列表“0,1,2,3,4”,我从 1..3 替换为“a,b,c,d,e”,结果为:“0,a,b,c, d,e,4"。

List<String> list = new ArrayList<>(Arrays.asList("0", "1", "2", "3", "4"));
List<String> subList = list.subList(1, 4);
subList.clear();
subList.addAll(Arrays.asList("a", "b", "c", "d", "e"));
System.out.println(list);

will output

会输出

[0, a, b, c, d, e, 4]

回答by Aroon

You can use: a1.subList(1, 3).clear(); It has already been discussed here:Why is Java's AbstractList's removeRange() method protected?It might help you to understand better.

您可以使用: a1.subList(1, 3).clear(); 这里已经讨论过:为什么 Java 的 AbstractList 的 removeRange() 方法受到保护?它可能会帮助您更好地理解。

回答by Ruben Vardanyan

removeRange(int firstIndex, int lastIndex)method is protectedmethod in ArrayList<Object>. Protected method is accessed in class, subclasses and in package, but not public.

removeRange(int firstIndex, int lastIndex)方法是 中的protected方法ArrayList<Object>。受保护的方法可以在类、子类和包中访问,但不是公共的。