索引范围的上限总是假定为排他的? [英] Are upper bounds of indexed ranges always assumed to be exclusive?

查看:127
本文介绍了索引范围的上限总是假定为排他的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



java.lang.String

因此,在Java中,每当给定一个索引范围时, / code>:


substring(int beginIndex,int endIndex)



返回一个新的字符串,它是这个字符串的子字符串。子字符串从指定的 beginIndex 开始,并扩展到索引 endIndex - 1的字符


java.util.Arrays



< >

copyOfRange(T [] original,int from,int to)



from - 要复制范围的初始索引,包括


java.util .BitSet


set(int fromIndex,int toIndex)



fromIndex - 要设置的第一个位的索引。

toIndex - 索引后的最后一位要设置。


正如你所看到的,它看起来像是Java试图使它的上限是排他的一致约定。



我的问题是:




  • 这是官方权威的建议吗?

  • 是否有值得注意的违规行为,我们应该警惕?

  • 这个系统有名称吗? (ala0-basedvs1-based)






<我完全理解在基于0的系统中 N 对象的集合被索引 0..N-1 。我的问题是,如果范围(2,4)给定,它可以是3项或2,取决于系统。



再次,问题不是第一个索引 0 最后索引 N-1 vs第一个索引 1 最后索引 N ;



问题是中有3个元素(2,4)vs(2,4)中有2个元素。

解决方案

对FredOverflow来说,这是一个正式认可的方法。称为半开放范围。因此,大概,Java集合可以被描述为基于0的基于半开放范围。



-open与其他地方的关闭范围:






siliconbrain.com - 16个使用半开范围的好理由(为了简洁起见):



  • 范围 [n,m)中的元素数量只是 mn (而不是 m-n + 1 )。

  • 空白范围是 [n,n )(而不是 [n,n-1] ,如果 n 是已经指向列表第一个元素的迭代器,或者如果 n == 0 )。

  • [13,42)(而不是 [13,41.999999999999] )。
  • 处理范围时, +1 -1 几乎从不使用。这是一个优点,如果它们是昂贵的(因为它是日期)。

  • 如果你在一个范围内写一个find,没有发现的事实可以很容易地通过返回结束作为找到的位置: if(find([begin,end))== end)没有找到

  • 启动数组下标为0(如C,C ++,JAVA,NCL)的上限等于大小。







半开和关闭范围


半开范围的优点:




  • 空白范围有效: [0 .. 0]

  • [x .. $]

  • 易于分割范围: [0..x] [x .. $]



关闭范围的优点:




  • 对称性。


  • ['a'...'z'] 不需要尴尬 + 1
  • uint.max]

最后一点是非常有趣。写 numberIsInRange(int n,int min,int max)如果 Integer.MAX_VALUE 可以在合法范围内。


So in Java, whenever an indexed range is given, the upper bound is almost always exclusive.

From java.lang.String:

substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1

From java.util.Arrays:

copyOfRange(T[] original, int from, int to)

from - the initial index of the range to be copied, inclusive
to - the final index of the range to be copied, exclusive.

From java.util.BitSet:

set(int fromIndex, int toIndex)

fromIndex - index of the first bit to be set.
toIndex - index after the last bit to be set.

As you can see, it does look like Java tries to make it a consistent convention that upper bounds are exclusive.

My questions are:

  • Is this the official authoritative recommendation?
  • Are there notable violations that we should be wary of?
  • Is there a name for this system? (ala "0-based" vs "1-based")

CLARIFICATION: I fully understand that a collection of N objects in a 0-based system is indexed 0..N-1. My question is that if a range (2,4) given, it can be either 3 items or 2, depending on the system. What do you call these systems?

AGAIN, the issue is not "first index 0 last index N-1" vs "first index 1 last index N" system; that's known as the 0-based vs 1-based system.

The issue is "There are 3 elements in (2,4)" vs "There are 2 elements in (2,4)" systems. What do you call these, and is one officially sanctioned over the other?

解决方案

Credit goes to FredOverflow in his comment saying that this is called the "half-open range". So presumably, Java Collections can be described as "0-based with half-open ranges".

I've compiled some discussions about half-open vs closed ranges elsewhere:


siliconbrain.com - 16 good reasons to use half-open ranges (edited for conciseness):

  • The number of elements in the range [n, m) is just m-n (and not m-n+1).
  • The empty range is [n, n) (and not [n, n-1], which can be a problem if n is an iterator already pointing the first element of a list, or if n == 0).
  • For floats you can write [13, 42) (instead of [13, 41.999999999999]).
  • The +1 and -1 are almost never used, when handling ranges. This is an advantage if they are expensive (as it is for dates).
  • If you write a find in a range, the fact that there was nothing found can easily indicated by returning the end as the found position: if( find( [begin, end) ) == end) nothing found.
  • In languages, which start the array subscripts with 0 (like C, C++, JAVA, NCL) the upper bound is equal to the size.


Half-open versus closed ranges

Advantages of half-open ranges:

  • Empty ranges are valid: [0 .. 0]
  • Easy for subranges to go to the end of the original: [x .. $]
  • Easy to split ranges: [0 .. x] and [x .. $]

Advantages of closed ranges:

  • Symmetry.
  • Arguably easier to read.
  • ['a' ... 'z'] does not require awkward + 1 after 'z'.
  • [0 ... uint.max] is possible.

That last point is very interesting. It's really awkward to write an numberIsInRange(int n, int min, int max) predicate with a half-open range if Integer.MAX_VALUE could be legally in a range.

这篇关于索引范围的上限总是假定为排他的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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