a long array

J

JurnChing

I want to make an array with a 'long' length, but seem like the
primitive array can only take length 'int'. For example:

public long a[];

public long func(long x) {
return a[x];
}

SomeClass.java:44: possible loss of precision
found : long
required: int
return a[x];
^

I am wondering if there is anyway to go around it without using the
Java Array class.

Thanks in advance
 
T

Tor Iver Wilhelmsen

I want to make an array with a 'long' length, but seem like the
primitive array can only take length 'int'.
Correct.

I am wondering if there is anyway to go around it without using the
Java Array class.

private long a[][] = new long[Integer.MAX_VALUE][Integer.MAX_VALUE];

public long get(long index) {
return a[index / Integer.MAX_VALUE][index % Integer.MAX_VALUE];
}

You get the idea.
 
A

Andrew Hobbs

JurnChing said:
I want to make an array with a 'long' length, but seem like the
primitive array can only take length 'int'. For example:

public long a[];

public long func(long x) {
return a[x];
}

SomeClass.java:44: possible loss of precision
found : long
required: int
return a[x];

Is there a real reason why you can't cast the array size to an int? I
assume that you don't actually want to produce an array of potentially that
size. ie with an int you can produce an array with 2,000,000,000 longs
which would require about 16 Gb of memory. With a long you could
potentially require 2,000,000,000 Gb of memory. Somewhat beyond most
peoples requirements.

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top