Path: csiph.com!usenet.pasdenom.info!goblin3!goblin.stu.neva.ru!newsfeed3.funet.fi!newsfeeds.funet.fi!feeder2.news.elisa.fi!uutiset.elisa.fi!7564ea0f!not-for-mail Newsgroups: comp.lang.java.programmer From: Heikki Kallasjoki Subject: Re: Oracle/Google demonstrate human beings cannot write 10 lines of code without making a mistake ;) References: <29308868.1994.1337265697084.JavaMail.geo-discussion-forums@pbcuc6> <84131$4fb54067$5419acc3$20839@cache90.multikabel.net> Organization: nonexistent User-Agent: slrn/pre1.0.0-18 (Linux) Message-ID: Lines: 46 Date: Thu, 17 May 2012 19:19:46 GMT NNTP-Posting-Host: 91.156.75.88 X-Complaints-To: newsmaster@saunalahti.com X-Trace: uutiset.elisa.fi 1337282386 91.156.75.88 (Thu, 17 May 2012 22:19:46 EEST) NNTP-Posting-Date: Thu, 17 May 2012 22:19:46 EEST Xref: csiph.com comp.lang.java.programmer:14592 On 2012-05-17, Heikki Kallasjoki wrote: > On 2012-05-17, Skybuck Flying wrote: >>>> private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) >>>> { >>>> if (fromIndex > toIndex) >>>> throw new IllegalArgumentException("fromIndex(" + fromIndex + >>>> ") > toIndex(" + toIndex+")"); >>>> if (fromIndex < 0) >>>> throw new ArrayIndexOutOfBoundsException(fromIndex); >>>> if (toIndex > arrayLen) >>>> throw new ArrayIndexOutOfBoundsException(toIndex); >>>> } >> >> It's simply not valid. Out of bounds has a very clear meaning in programming >> practice. It's either within bounds or it's not. The bounds of a java array > > Yes, but it is not designed for testing whether fromIndex and toIndex > are within those bounds. It is used to test whether fromIndex, > fromIndex+1, ..., toIndex-1 are, which it does. In particular, the > element with index "toIndex" is not part of the range it's testing, so > it does not need to be inside the array bounds. For "proof", see the original comments that accompany the rangeCheck function: /** * Checks that fromIndex and toIndex are in range, and throws an * appropriate exception if they aren't. * * @param arrayLen the length of the array * @param fromIndex the index of the first element of the range * @param toIndex the index after the last element of the range * @throws IllegalArgumentException if fromIndex > toIndex * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 * or toIndex > arrayLen */ Note in particular the words "the index after the last element of the range". (Source: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/TimSort.java?view=co ) -- Heikki Kallasjoki