Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Using Enumerated Types as Array Indexes Date: Wed, 17 Aug 2011 16:09:29 -0700 Organization: A noiseless patient Spider Lines: 25 Message-ID: References: <4e4b0d81$0$314$14726298@news.sunsite.dk> <4e4b1fe4$0$314$14726298@news.sunsite.dk> <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> <9b2aglFuj7U1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 17 Aug 2011 23:09:39 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="4732"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/guBjoufwMcV4MhTu5ESDNo2BrgExJE/A=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: <9b2aglFuj7U1@mid.individual.net> Cancel-Lock: sha1:/MuKb4u6Cccc5qCFq6Rq6Nh8t/s= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7167 On 8/17/2011 10:02 AM, Robert Klemme wrote: > 1. There was an AtomicInteger initialized at startup with 0 which for > every request coming into that system was incremented in a thread safe > manner (incrementAndGet()). I'd be shocked if an int actually overflowed when counting requests like this, but that's all I can think of. 2^31 is such a huge number, I didn't think you could get there in practical cases. > 2. The result was used to index into an array with the quite obvious > "arr[n % arr.length]". > > 3. (left as exercise for the reader) Assuming that the AtomicInteger did overflow, then n % y produces negative numbers if n is negative, a clear AIOOB. Try : arr[ Math.abs( n ) % arr.length ];