Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short answer is: Use</p> <pre><code>al.subList(1, 3).clear(); </code></pre> <hr> <p>The <code>removeRange(int, int)</code> method is <code>protected</code>. You can only invoke it from a subclass of <code>ArrayList</code> or from a class within the same package as <code>ArrayList</code>. See <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html" rel="nofollow noreferrer">Controlling Access to Members of a Class</a>.</p> <p>The only way to access the <code>removeRange</code> method is to subclass <code>ArrayList</code> and make the method public. E.g.</p> <pre><code>public class RangeRemoveSupport&lt;E&gt; extends ArrayList&lt;E&gt; { public void removeRange(int fromIndex, int toIndex) { super.removeRange(fromIndex, toIndex); } } </code></pre> <p>But then your code must use the subclass. Thus your code depends on this subclass and not just depends on <code>List</code> or <code>ArrayList</code>.</p> <p>A utility class within the same package to access it is not possible. E.g.</p> <pre><code>package java.util; // &lt;- SecurityException public class RemoveRangeSupport { public static void removeRange(ArrayList&lt;?&gt; list, int from, int to){ list.removeRange(from, to); } } </code></pre> <p>This will cause a <code>SecurityException</code> </p> <blockquote> <p><strong><code>java.lang.SecurityException: Prohibited package name: java.util</code></strong>.</p> </blockquote> <p>because you are not allowed to define classes in <code>java.util</code> for security reasons.</p> <p>Nevertheless for other packages it might be a way.</p> <p>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.</p> <p><strong>EDIT</strong></p> <blockquote> <p>Is there perhaps a function to replace items from range X..Y, to new items of a possible different size? </p> <p>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". </p> </blockquote> <pre><code>List&lt;String&gt; list = new ArrayList&lt;&gt;(Arrays.asList("0", "1", "2", "3", "4")); List&lt;String&gt; subList = list.subList(1, 4); subList.clear(); subList.addAll(Arrays.asList("a", "b", "c", "d", "e")); System.out.println(list); </code></pre> <p>will output</p> <pre><code>[0, a, b, c, d, e, 4] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload