Understanding the Removerange(_:) Documentation

Understanding the removeRange(_:) documentation

Swift 2

We're going to use var since removeRange needs to operate on a mutable string.

var welcome = "hello there"

This line:

let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex

means that we start from the end of the string (welcome.endIndex) and move back by 6 characters (advance by a negative number = move back), then ask for the range (..<) between our position and the end of the string (welcome.endIndex).

It creates a range of 5..<11, which encompasses the " there" part of the string.

If you remove this range of characters from the string with:

welcome.removeRange(range)

then your string will be the remaining part:

print(welcome) // prints "hello"

You can take it the other way (from the start index of the string) for the same result:

welcome = "hello there"
let otherRange = welcome.startIndex.advancedBy(5)..<welcome.endIndex
welcome.removeRange(otherRange)
print(welcome) // prints "hello"

Here we start from the beginning of the string (welcome.startIndex), then we advance by 5 characters, then we make a range (..<) from here to the end of the string (welcome.endIndex).

Note: the advance function can work forward and backward.

Swift 3

The syntax has changed but the concepts are the same.

var welcome = "hello there"
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
print(welcome) // prints "hello"

welcome = "hello there"
let otherRange = welcome.index(welcome.startIndex, offsetBy: 5)..<welcome.endIndex
welcome.removeSubrange(otherRange)
print(welcome) // prints "hello"

Effective Java Item 17: How can overriding removeRange() improve performance?

Let's take a concrete example - suppose that your implementation is backed by a dynamic array (this is how ArrayList works, for example). Now, suppose that you want to remove elements in the range [start, end). The default implementation of removeRange works by getting an iterator to position start, then calling remove() the appropriate number of times.

Each time remove() is called, the dynamic array implementation has to shuffle all the elements at position start + 1 and forward back one spot to fill the gap left in the removed element. This could potentially take time O(n), because potentially all of the array elements might need to get shuffled down. This means that if you're removing a total of k elements from the list, the naive approach will take time O(kn), since you're doing O(n) work k times.

Now consider a much better approach: copy the element at position end to position start, then element end + 1 to position start + 1, etc. until all elements are copied. This requires you to only do a total of O(n) work, because every element is moved at most once. Compared with the O(kn) approach given by the naive algorithm, this is a huge performance improvement. Consequently, overriding removeRange to use this more efficient algorithm can dramatically increase performance.

Hope this helps!

Why removeRange method of ArrayList class is not working?

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



Related Topics



Leave a reply



Submit