Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using Enumerated Types as Array Indexes

Reply
Thread Tools

Using Enumerated Types as Array Indexes

 
 
David Lamb
Guest
Posts: n/a
 
      08-17-2011
On 16/08/2011 8:38 PM, Arne Vajh°j wrote:
> On 8/16/2011 10:53 AM, KevinSimonson wrote:
>> Java is a pretty handy language in its own right. But in Ada one
>> could define arrays to be indexed by enumerated types. Can Java do
>> that? If not, why not?

>
> Java is a simpler language than Ada. In Java array indexes are
> int and you have byte/short/int/long types and that is it.


Sure -- but if Enums had been in the language from the beginning, then
it might have occurred to people that array indexes could be "any type
with a method 'int ordinal()'" (with the appropriate caveat about being
in range 0..length-1)
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      08-17-2011
Arved Sandstrom <(E-Mail Removed)> wrote:
> On 11-08-17 12:45 AM, Lew wrote:
>> On Tuesday, August 16, 2011 8:20:05 PM UTC-7, markspace wrote:
>>> On 8/16/2011 6:56 PM, Arne Vajh├»┬┐┬Żj wrote:
>>>> byte/short/int/long integer types
>>> OK, you mean for indexes? char works too, and long really doesn't -- it
>>> has to be cast to an int. [...]

>> JLS 10: [...]
>> IOW, you never index arrays with a byte, char or short. [...]

> I'm feeling slow this morning: how do we get problems with byte->int,
> short->int, or char->int again?


array-indexing takes integers. That you can also throw byte/short/char
values at them without cast doesn't mean that arrays "take" those, but
only that such values get automatically converted to int before being
taken by the array for indexing.

An example for something really "taking" byte-values:
foo(byte b) { ... }

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-17-2011
On 17.08.2011 14:29, David Lamb wrote:
> On 16/08/2011 8:38 PM, Arne Vajh°j wrote:
>> On 8/16/2011 10:53 AM, KevinSimonson wrote:
>>> Java is a pretty handy language in its own right. But in Ada one
>>> could define arrays to be indexed by enumerated types. Can Java do
>>> that? If not, why not?

>>
>> Java is a simpler language than Ada. In Java array indexes are
>> int and you have byte/short/int/long types and that is it.

>
> Sure -- but if Enums had been in the language from the beginning, then
> it might have occurred to people that array indexes could be "any type
> with a method 'int ordinal()'" (with the appropriate caveat about being
> in range 0..length-1)


Or we had an interface Countable { int ordinal(); } which Enum would
implement and which could be used for array indexing. Or... I don't
find it worthwhile to speculate about what could have happened if
something else happened.

And please also notice that there is still EnumMap as Patricia pointed
out - type safe, efficient and all. Just because Ada does it this way
doesn't mean that it's the best way or that other languages must follow
suit.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-17-2011
Andreas Leitgeb wrote:
> Arved Sandstrom <(E-Mail Removed)> wrote:
>> Lew wrote:
>>> JLS 10: [...]
>>> IOW, you never index arrays with a byte, char or short. [...]

>> I'm feeling slow this morning: how do we get problems with byte->int,
>> short->int, or char->int again?


One gets problems with byte->int and short->int with array indexes the way one gets into trouble with those widening conversions generally. The usualsuspect is the lack of unsigned versions, so widening a (byte)0xA0, for example, would result in a negative index.

One gets into trouble generally in programming when one thinks one thing isgoing on ("index takes a byte") whilst ignoring what's really going on (there's a widening conversion involved). You might get away with it most of the time, but occasionally such things trip you up.

Why think about it imprecisely? Tell us the advantage of that, please.

> array-indexing takes integers. That you can also throw byte/short/char
> values at them without cast doesn't mean that arrays "take" those, but
> only that such values get automatically converted to int before being
> taken by the array for indexing.
>
> An example for something really "taking" byte-values:
> foo(byte b) { ... }


--
Lew
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-17-2011
On 17.08.2011 16:17, Lew wrote:
> Andreas Leitgeb wrote:
>> Arved Sandstrom<(E-Mail Removed)> wrote:
>>> Lew wrote:
>>>> JLS 10: [...] IOW, you never index arrays with a byte, char or
>>>> short. [...]
>>> I'm feeling slow this morning: how do we get problems with
>>> byte->int, short->int, or char->int again?

>
> One gets problems with byte->int and short->int with array indexes
> the way one gets into trouble with those widening conversions
> generally. The usual suspect is the lack of unsigned versions, so
> widening a (byte)0xA0, for example, would result in a negative
> index.
>
> One gets into trouble generally in programming when one thinks one
> thing is going on ("index takes a byte") whilst ignoring what's
> really going on (there's a widening conversion involved). You might
> get away with it most of the time, but occasionally such things trip
> you up.


Why had a nice issue recently which exactly fits this bill: after over
two months of uninterrupted, completely error free production usage the
application suddenly stopped working giving weird error messages (there
was an issue with the error reporting as well, but let's ignore that for
the moment). Turns out this was the setup:

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()).

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)

Anybody who now thinks all is fine should stop coding Java immediately
and go reading the language spec.

> Why think about it imprecisely? Tell us the advantage of that,
> please.


Some may reason: Someone else might have to pay for the laziness. But
then again, the risk is it might come right back haunting the
originator. Well, no risk, no fun.

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      08-17-2011
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 ];



 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      08-17-2011
markspace <-@> wrote:
> 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.


Depends of course on the average number of requests per second:
at about 800, you'll get first overflow roughly within a month.

>> 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 ];


What was Math.abs(-214748364 again?

Rather: arr[ (n%arr.length + arr.length) % arr.length ]
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      08-18-2011
On 8/17/2011 4:48 PM, Andreas Leitgeb wrote:
>
> What was Math.abs(-214748364 again?
>
> Rather: arr[ (n%arr.length + arr.length) % arr.length ]



Ah yes, good point. I'll have to keep that in mind.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-18-2011
On 8/17/2011 1:02 PM, Robert Klemme wrote:
> On 17.08.2011 16:17, Lew wrote:
>> Andreas Leitgeb wrote:
>>> Arved Sandstrom<(E-Mail Removed)> wrote:
>>>> Lew wrote:
>>>>> JLS 10: [...] IOW, you never index arrays with a byte, char or
>>>>> short. [...]
>>>> I'm feeling slow this morning: how do we get problems with
>>>> byte->int, short->int, or char->int again?

>>
>> One gets problems with byte->int and short->int with array indexes
>> the way one gets into trouble with those widening conversions
>> generally. The usual suspect is the lack of unsigned versions, so
>> widening a (byte)0xA0, for example, would result in a negative
>> index.
>>
>> One gets into trouble generally in programming when one thinks one
>> thing is going on ("index takes a byte") whilst ignoring what's
>> really going on (there's a widening conversion involved). You might
>> get away with it most of the time, but occasionally such things trip
>> you up.

>
> Why had a nice issue recently which exactly fits this bill: after over
> two months of uninterrupted, completely error free production usage the
> application suddenly stopped working giving weird error messages (there
> was an issue with the error reporting as well, but let's ignore that for
> the moment). Turns out this was the setup:
>
> 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()).
>
> 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)
>
> Anybody who now thinks all is fine should stop coding Java immediately
> and go reading the language spec.


Yes, but what has this error to do with widening conversions,
or conversions of any kind? It's GIGO, pure and simple.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      08-18-2011
On 8/16/2011 11:20 PM, markspace wrote:
> On 8/16/2011 6:56 PM, Arne Vajh°j wrote:
>> byte/short/int/long integer types

>
> OK, you mean for indexes?


No, in general.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Name Enumerated Types and Structs Immortal Nephi C++ 4 08-24-2010 05:20 PM
Assigning Values to Enumerated Types Kevin Neilson VHDL 9 05-09-2008 05:19 PM
size of a class having enumerated data types? ypjofficial@indiatimes.com C++ 8 01-23-2006 12:29 PM
Progressive restriction on enumerated types in Owl David R. Throop XML 0 05-05-2005 11:39 PM
strange recursivion with enumerated types - how is this possible? rfractal30 Java 6 02-25-2005 02:38 AM



Advertisments