Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Creating a byte[] of long size (http://www.velocityreviews.com/forums/t727757-creating-a-byte-of-long-size.html)

 Boris Punk 07-08-2010 08:30 PM

Creating a byte[] of long size

long size = Integer.MAX_VALUE+1;
byte [] b = new byte[size];

-possible loss of precision

How can we make an array of long size?

 Eric Sosman 07-08-2010 08:52 PM

Re: Creating a byte[] of long size

On 7/8/2010 4:30 PM, Boris Punk wrote:
> long size = Integer.MAX_VALUE+1;
> byte [] b = new byte[size];
>
> -possible loss of precision
>
> How can we make an array of long size?

Array elements are indexed from [0] through [Integer.MAX_VALUE-1],
and that's that. You can make an array whose elements are larger
than a single byte (`new long[N]', for example) and thus get an array
of more than Integer.MAX_VALUE-1 bytes, but you cannot have more than
that many elements.

Incidentally, you might want to print the value of `size' that
you calculate in your example. It might surprise you ...

(By the way: Yes, I really did intend the `-1's above. An int
index value could go one higher, but how could you create the array?
In `new byte[N]', N cannot exceed Integer.MAX_VALUE, which means that
the index cannot exceed N-1, or Integer.MAX_VALUE-1. You could try
to write `new byte[] { 0,0,0,... }' and so on for two giga-values,
but you'd exceed .class file limits long before you got there.)

--
Eric Sosman
esosman@ieee-dot-org.invalid

 Eric Sosman 07-08-2010 08:55 PM

Re: Creating a byte[] of long size

On 7/8/2010 4:52 PM, Eric Sosman wrote:
> On 7/8/2010 4:30 PM, Boris Punk wrote:
>> long size = Integer.MAX_VALUE+1;
>> byte [] b = new byte[size];
>>
>> -possible loss of precision
>>
>> How can we make an array of long size?

>
> Array elements are indexed from [0] through [Integer.MAX_VALUE-1],
> and that's that. You can make an array whose elements are larger
> than a single byte (`new long[N]', for example) and thus get an array
> of more than Integer.MAX_VALUE-1 bytes, but you cannot have more than
> that many elements.
> [...]
> (By the way: Yes, I really did intend the `-1's above.[...]

Oh, drat. I really did mean the first one, but not the second.
It's been really hot here for the last few days, and my brain is
starting to resemble a poached egg.

--
Eric Sosman
esosman@ieee-dot-org.invalid

 Lew 07-08-2010 09:15 PM

Re: Creating a byte[] of long size

Boris Punk wrote:
> long size = Integer.MAX_VALUE+1;
> byte [] b = new byte[size];
>
> -possible loss of precision
>
> How can we make an array of long size?
>

You can't.

Why do you want to?

From the JLS, which I strongly urge you to study:
"The type of each dimension expression within a DimExpr must be a type
that is convertible (§5.1.8) to an integral type, or a compile-time
error occurs. Each expression undergoes unary numeric promotion (§).
The promoted type must be int, or a compile-time error occurs; this
means, specifically, that the type of a dimension expression must not
be long."
15.10 Array Creation Expressions

Didn't you get a compiler error?

You can make some other data structure that would hold that much,
assuming you have the address space for it. For sure in a 32-bit
machine you'd have trouble even with 'new byte [Integer.MAX_VALUE]'.

But really, why do you want to?

--
Lew

 Boris Punk 07-08-2010 09:35 PM

Re: Creating a byte[] of long size

Integer.MAX_VALUE = 2147483647

I might need more items than that. I probably won't, but it's nice to have
extensibility.

 Eric Sosman 07-08-2010 10:15 PM

Re: Creating a byte[] of long size

On 7/8/2010 5:35 PM, Boris Punk wrote:
> Integer.MAX_VALUE = 2147483647
>
> I might need more items than that. I probably won't, but it's nice to have
> extensibility.

Then Java is not the language for you. Arrays have int sizes.
Not even Collections can get you out of the woods, because their
..size() method must return an int.

I suppose you could write your own BigList class, with a .size()
method returning long and with .get() and .set() taking long arguments.
Such a class could not implement the List interface (because the method
signatures wouldn't be right), but you could probably implement
Iterable.

Or, you could have BigList implement List but "lie" in its .size()
method, in somewhat the same way TreeSet "lies" about the Set contract.
Then you'd add .realSize(), .realGet(), and .realSet() methods to deal
with the long values (I've probably missed a few).

... but I think you'll need a stronger motivation than "it's nice"
to justify the work, and the resulting ugliness. Your call, though.

--
Eric Sosman
esosman@ieee-dot-org.invalid

 Boris Punk 07-08-2010 10:22 PM

Re: Creating a byte[] of long size

"Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
news:i15imo\$88e\$1@news.eternal-september.org...
> On 7/8/2010 5:35 PM, Boris Punk wrote:
>> Integer.MAX_VALUE = 2147483647
>>
>> I might need more items than that. I probably won't, but it's nice to
>> have
>> extensibility.

>
> Then Java is not the language for you. Arrays have int sizes.
> Not even Collections can get you out of the woods, because their
> .size() method must return an int.
>
> I suppose you could write your own BigList class, with a .size()
> method returning long and with .get() and .set() taking long arguments.
> Such a class could not implement the List interface (because the method
> signatures wouldn't be right), but you could probably implement
> Iterable.
>
> Or, you could have BigList implement List but "lie" in its .size()
> method, in somewhat the same way TreeSet "lies" about the Set contract.
> Then you'd add .realSize(), .realGet(), and .realSet() methods to deal
> with the long values (I've probably missed a few).
>
> ... but I think you'll need a stronger motivation than "it's nice"
> to justify the work, and the resulting ugliness. Your call, though.
>
> --
> Eric Sosman
> esosman@ieee-dot-org.invalid

Is there no BigList/BigHash in Java?

 Arne Vajhøj 07-08-2010 10:49 PM

Re: Creating a byte[] of long size

On 08-07-2010 18:22, Boris Punk wrote:
> Is there no BigList/BigHash in Java?

No.

But You can have a List<List<X>> which can then
store 4*10^18 X'es.

Arne

 Arne Vajhøj 07-08-2010 11:22 PM

Re: Creating a byte[] of long size

On 08-07-2010 17:35, Boris Punk wrote:
> Integer.MAX_VALUE = 2147483647
>
> I might need more items than that. I probably won't, but it's nice to have
> extensibility.

It is a lot of data.

I think you should assume YAGNI.

Arne

 Arne Vajhøj 07-08-2010 11:55 PM

Re: Creating a byte[] of long size

On 08-07-2010 17:15, Lew wrote:
> From the JLS, which I strongly urge you to study:

Unless the poster has a solid programming experience,
then the JLS may not be the best to study.

Sure it is by definition correct, but it is written
to be detailed and correct not to be easy to read.

Arne

All times are GMT. The time now is 12:13 PM.