0

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

Edd
  • 3,724
  • 3
  • 26
  • 33
Fawad Khalil
  • 357
  • 3
  • 20
  • 1
    Is it possible to provide the code you've tried. This will A) help people to fully understand the requirements, and B) ensure that answers provided do what you want. – Edd Mar 19 '14 at 09:59
  • @Edd How did your answer not solve the question? That OP wants isn't possible. – christopher Mar 19 '14 at 10:05
  • @Christopher When I edited the question I re-read it and my understanding of it changed from "How do I have an array of `n` elements where `n > Integer.MAX_VALUE`" to something more like "I want to be able to store more elements than an `array` can; how can I get my jlist-y thing to deal with some form of 2D array". I tried not to suggest one interpretation or another in my edit. I might undelete my answer in case it's of use. – Edd Mar 19 '14 at 10:51

2 Answers2

2

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.

Community
  • 1
  • 1
Edd
  • 3,724
  • 3
  • 26
  • 33
  • This *does* answer the question. OP wants to know if they can specify an array with dimensions the size of `long`, when the Java documentation clearly states that the largest size an `Array` can take is `Integer.MAX_VALUE`. Perhaps an Object based solution might work, but other than that I'm unsure. – christopher Mar 19 '14 at 10:57
1

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.

Community
  • 1
  • 1
Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
  • There has been some suggestions to change the compiler and allowing for long-indexed arrays: http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000869.html and http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000863.html – Daniel Figueroa Mar 19 '14 at 10:29