I need to create a large integer array but I am not able to increase its size beyond max int value.
How do I create larger arrays?

I don't believe an array in Java can be larger than MAX_INT elements because arrays are indexed by ints. Of course you could use a linked list or some other structure that doesn't rely on indices, but if you absolutely must have an array, you could create your own class, say BigArray, that actually contains multiple arrays. You just have to figure out which array to look at based on the index. I don't think you'll be able to use square brackets to index though because Java doesn't have operator overloading like C++. So you'll have to rely on method calls to index into your BigArray.

Comments
Short, precise and to the point.

Is this an assignment that you have or a possible solution to your problem? If it is the latter it's possible that there are better solutions - would you like to post the problem?

I need to create a large integer array but I am not able to increase its size beyond max int value.
How do I create larger arrays?

Do you know what is the size of the array?
What compiler you are working on? [2/4/8 - bit compiler]
If you are working on a 2-bit compiler, say sdcc, integer holds a maximum number of 2^16. So if your array is larger than this create a 2-D array and access it as a 2-D array or with properly casted pointers.

#include <stdio.h>
#define MAX (-1L)
#define NUM 0x55	//any number of your choice
main()
{
//Assuming a 2 - byte compiler like sdcc

	//creating an array of 65535 * 65535 integers 
	int arr[MAX][MAX];	// sizeof (arr) = 65535 * 65535 * sizof(int)

	int row;
	int col;

	for (row = 0; row < MAX; row++)
		for (col = 0; col < MAX; col++)
			arr[row][col] = NUM;

	return 0;
}

Hope this helps!

Cheers

ArrayList may or may not be able to hold an array size larger than max int. Remember that everything is holding in memory, and Java has its maximum memory for the use. Therefore, it is limited by your computer memory.

If you want to hold an array size larger than max in (presumably it is possible), ArrayList may be use. Though, you could use a multi-dimensional array as well.

Do you know what is the size of the array?
What compiler you are working on? [2/4/8 - bit compiler]
If you are working on a 2-bit compiler, say sdcc, integer holds a maximum number of 2^16. So if your array is larger than this create a 2-D array and access it as a 2-D array or with properly casted pointers.

Cheers

Thanks for the reply but I need to do it in java..

Is this an assignment that you have or a possible solution to your problem? If it is the latter it's possible that there are better solutions - would you like to post the problem?

This is not an assignment and possibly not even the solution to my problem.

I need to find the time taken for sorting nos. by some specified algorithms on different array sizes.

So I dont think 2-d array can sovle my problem.

If I use array list I wont be able to access the elements through their index and swap them etc...

You can access elements in an ArrayList by their index - just use get. But ArrayList actually uses an array behind the scenes, so you are still limited to max int for the size, in addition to being limited by the amount of available memory on your system. While the latter may be fixable by adding more memory and/or telling the JVM to use a bigger heap, the former is a hard limit that you can't get around.

Using an n-dimensional array is similar to the solution I first proposed, and in theory, either of these should work.

You say you are timing sorting algorithms on different sized arrays. Do you really need an array with more than 2 billion items?

While the latter may be fixable by adding more memory and/or telling the JVM to use a bigger heap, the former is a hard limit that you can't get around.

You say you are timing sorting algorithms on different sized arrays. Do you really need an array with more than 2 billion items?

Actually I am not even able to reach 2 billion items. Max I am able to make is an array of size 40 million. After that it gives a heap memory error. How could I increase the memory which JVM uses?
I am using eclipse.
Even if I am able to make an array of 1 billion elements I would be able to do a fairly good comparison I guess..

There are 2 flags you can pass to the JVM: java -Xms<initial heap size> -Xmx<maximum heap size> According to this article, the defaults are (initial) 32MB and (max) 128 MB. So if you pass larger numbers to the JVM, you will have more memory allocated to the heap.

In eclipse, you can do this using the "Run Configurations" tool. If you click the down arrow next to the green GO button, you will see this option. When you bring up the tool, choose the program you want to run on the left. Then on the right, the 2nd tab says Arguments. You want to fill in the box for VM arguments. In that box you can just type "-Xms32m -Xmx128m", but of course put the new numbers you want.

Also see this article about common mistakes you don't want to make.

There are 2 flags you can pass to the JVM: java -Xms<initial heap size> -Xmx<maximum heap size> According to this article, the defaults are (initial) 32MB and (max) 128 MB. So if you pass larger numbers to the JVM, you will have more memory allocated to the heap.

In eclipse, you can do this using the "Run Configurations" tool. If you click the down arrow next to the green GO button, you will see this option. When you bring up the tool, choose the program you want to run on the left. Then on the right, the 2nd tab says Arguments. You want to fill in the box for VM arguments. In that box you can just type "-Xms32m -Xmx128m", but of course put the new numbers you want.

Also see this article about common mistakes you don't want to make.

This solved my problem.. Thankyou so much :)

for your assignment, you may use the principle of External Sort,,,, such that you are to divide your elements into two or three,,, groups and load the into the memory sequentially from a file,,,, this may help,,, because the I/O time will be the same for all sorts,,

This question has already been answered. Start a new discussion instead.