Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why removeRange method of ArrayList class is not working? [duplicate]

Tags:

java

arraylist

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)

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?

like image 627
Chaitanya Avatar asked Dec 23 '13 18:12

Chaitanya


2 Answers

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 ArrayList or from a class within the same package as ArrayList. See Controlling Access to Members of a Class.

The only way to access the removeRange method is to subclass ArrayList and make the method public. E.g.

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 List or 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

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

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

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".

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]
like image 111
René Link Avatar answered Oct 08 '22 06:10

René Link


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.

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

like image 33
Suresh Atta Avatar answered Oct 08 '22 04:10

Suresh Atta