Fawad Khalil
Fawad Khalil

Reputation: 367

How to set array dimension to long number

I have created a jtable in Java that fetches its contents from a database table. It is possible that number of rows might exceed the capacity of the int datatype in Java as the database table's primary key has been set to bigint datatype (SQL Server 2008).

What I want to do is create a 2D array that would hold n rows of data where n is of long datatype. This array of data would be passed to jtable's model. I tried to declare array object without providing rows number and providing columns number only but it gave syntax error. May be I made a mistake in declaration method. If there is any method of such type declaration then please tell me the syntax or if not then please tell me the solution to solve it. The array is holding data of Object type (i.e. it is an Object[] array).

Upvotes: 0

Views: 497

Answers (2)

Daniel Figueroa
Daniel Figueroa

Reputation: 10666

You can't set an array to a size larger than Integer.MAX_VALUE, the java specs has the following to say:

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.

Thus you can't create an array with a size larger than the maxvalue of integers. The why I don't know, but my guess is that it has something to do with optimization. So it seems that you have to go with an ArrayList.

Upvotes: 1

Edd
Edd

Reputation: 3822

According to the Java Language Specification section on Array Access Expressions:

The index expression undergoes unary numeric promotion (§5.6.1). The promoted type must be int, or a compile-time error occurs.

Another answer on Stackoverflow confirms that it is not possible to have more slots than an int can offer.

Upvotes: 2

Related Questions