• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does array index have to be an integer?

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Java language specification :[Reference: Arrays: 10.4 Array Access ]

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (ยง5.6.1) and become int values. An attempt to access an array component with a long index value results in a compile-time error.


Why does array index have to be an integer and integer only?

I guess it has got something to do with ease of adressing(?)..but I want a more reliable and exact answer.

Please help.

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it has to be a countable set of symbols, because the index refers to the individual discrete cells of an array, ordered from one end to the other. If floating point numbers were allowed, how would you handle the fractional part?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why int and not long.

The designers probably thought that an Array will never exceed 2,147,483,647 elements.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Well, it has to be a countable set of symbols, because the index refers to the individual discrete cells of an array, ordered from one end to the other. If floating point numbers were allowed, how would you handle the fractional part?


Thanks for you reply EFH..

I understand what you say; but then there are associative arrays which use arbitrary indices and are not directly supported by Java.

My question is more like - why did the designers prefer only integers? Does using integers help, in any way, with providing better/faster access(calculating the next addressable location etc.) or Is it just part of the spec?

 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect using ints dates back to the time of C when you found the addressable location by adding the index to the location of the array. It is awkward to find the next member by adding a floating-point number, but very fast by adding an integer. You can do it at the chip level with relative addressing.
I presume they chose int rather than long because they thought nobody would need an array of ≥ 2^31 elements. Or even to prohibit anybody using an array with 2^31 elements.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I suspect using ints dates back to the time of C when you found the addressable location by adding the index to the location of the array. It is awkward to find the next member by adding a floating-point number, but very fast by adding an integer. You can do it at the chip level with relative addressing.
I presume they chose int rather than long because they thought nobody would need an array of ≥ 2^31 elements. Or even to prohibit anybody using an array with 2^31 elements.


Many thanks CR..

[I could be totally wrong here : ]
From what I remember,in C, the name of the array refers(or points) to the location in memory from where the array starts. The indexes are used to increment the pointer variable and thereby, access the memory location where the respective array elelments are stored. For e.g: a[1],a[2] are *(a++) and *((a++)++) respectively. For a 32- bit platform, the pointer variable is of 4 bytes, so having a integer(4 bytes also) as index probably helps?

Please correct me if I am wrong...

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can tell, your C code is correct.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:Why int and not long.

The designers probably thought that an Array will never exceed 2,147,483,647 elements.

I just tried writing a class called JumboArray that used long for indexing. It used a three dimensional array:
- the top level dimension had a size of length() / Integer.MAX_VALUE / Integer.MAX_VALUE, rounding up to 1 if it was 0.
- the dimension below that had a size of (length() / Integer.MAX_VALUE) % Integer.MAX_VALUE, rounding up to 1 if it was 0.
- the dimension below that had up to Integer.MAX_VALUE elements

This jumbo array could be indexed as follows:
I completed it, then tried testing with Long.MAX_VALUE as the size: java.lang.OutOfMemoryError. Specifying Integer.MAX_VALUE as the size: java.lang.OutOfMemoryError. I tried setting the -Xmx flag to 1024MB - still java.lang.OutOfMemoryError. Whatever I chose, java.lang.OutOfMemoryError. (Also caused by a limitation of the flag.) I had to go down to Integer.MAX_VALUE >> 8 as the size before it accepted it.

That makes sense of course, since Integer.MAX_VAlUE already is 2GB - if the elements are bytes. If the elements are int that's already 8GB. For Object, that's (probably) 8GB plus the size of the objects. And that was excluding the overhead for the array(s).

So yeah, Integer.MAX_VALUE seems like a reasonable upper bound Be glad they didn't choose short or char...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

An "associative array" like in Perl or Ruby is not an array at all, in the older comp-sci sense. An array is, by definition, a linear section of memory addressed in order. Arrays are far older than C, of course: FORTRAN, one of the very first compiled languages, certainly had them. In Java an associative array is called a Map, and although there's no direct language support for them, there are many Map types in the API.

Pascal arrays could use an enumeration as an index, so you could have an array whose indices were the seven colors of the rainbow, for instance. Underneath, the seven names would be compiled down to the first seven integers, but the compiler type-checked so you could only use those seven values in code.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simplicity is probably the most correct answer.

Regardless, if you have a program that has several billion items to store, an array of any type is the wrong data structure to be using. There is a reason disk based data structures were invented. Of course this gets into how databases are actually implemented and likely further than you need to go.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic