Java creating augmented array of size 400,000,000Max heap in JavaOptimising Funny MarblesCreate a struct to store student data and perform statistical analysis on dataSimple multi-dimensional Array class in C++11 - follow-upArray class to replace all iterator needsData Table Report ClassImproving this TileMapSwift function to find a specific set of combinations of 3 digits within a larger integer arrayEEPROM-based filesystem, aiming for Misra C++ 2008 complianceMaxCounters solution in C# from Codility
Suspicious crontab entry running 'xribfa4' every 15 minutes
What's the most efficient way to draw this region?
Giving a character trauma but not "diagnosing" her?
How to respond when insulted by a grad student in a different department?
How can my hammerspace safely "decompress"?
Is consistent disregard for students' time "normal" in undergraduate research?
Slow coworker receiving compliments while I receive complaints
Short story about aliens who tried using the common cold as a weapon
How to protect my Wi-Fi password from being displayed by Android phones when sharing it with QR code?
Match the blocks
Is it realistic that an advanced species isn't good at war?
Is data science mathematically interesting?
What's the current zodiac?
What are the consequences for downstream actors of redistributing a work under a wider CC license than the copyright holder authorized?
Why are seats at the rear of a plane sometimes unavailable even though many other seats are available in the plane?
What is the word for things that work even when they aren't working (e.g. escalators)?
Can the bass be used instead of drums?
How did Ron get five hundred Chocolate Frog cards?
Why is lying to Congress a crime?
How safe is using non-RoHS parts?
Are There 3D Rules for Flying and Distance?
R or Cpp for some finance work involved complex numbers?
Disrespectful employee going above my head and telling me what to do. I am his manager
How should I tell a professor the answer to something he doesn't know?
Java creating augmented array of size 400,000,000
Max heap in JavaOptimising Funny MarblesCreate a struct to store student data and perform statistical analysis on dataSimple multi-dimensional Array class in C++11 - follow-upArray class to replace all iterator needsData Table Report ClassImproving this TileMapSwift function to find a specific set of combinations of 3 digits within a larger integer arrayEEPROM-based filesystem, aiming for Misra C++ 2008 complianceMaxCounters solution in C# from Codility
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
$begingroup$
I was solving one competitive coding question having integer bounds 2^100. Fortunately, it was a dp question and I didn't need an array of that size. Practically we cannot create an array of size more than Integer.MAX_VALUE.
But I was thinking of creating a multidimensional array and treat it as a one-dimensional array.
what I need was a very large array with O(1) retrieval time. So Internally it will be a multidimensional array but I will treat it as a one-dimensional array.
So the size of the resultant array will be n*n.
if n is Integer.MAX_VALUE then the resultant augmented array will be of Integer.MAX * Integer.MAX
Here what I did to implement this idea.
BigArray class
public class BigArray
private int[][] arr;
private int row=0;
private int col = 0;
private int size;
public BigArray(int size)
arr = new int[size][size];
this.size = size;
add method
public void add(int data)
if(row > size-1)
col++;
row=0;
this.arr[col][row] = data;
row++;
else
this.arr[col][row] = data;
row++;
get method
public int get(int pos)
//get value in O(1)
if((int) Math.sqrt(pos) > size)
return -1;
int col1 = pos/(size);
int row1 = pos%(size);
return arr[col1][row1];
But If I pass Integer.MAX_VALUE in BigArray it throws Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
maximum size I've tried is 20,000 so resultant size will be 20,000 x 20,000 = 400,000,000.
But if add one more dimension to this array I'll get more space.
if BigArray has arr[][][] resultant size will be n x n x n;
if BigArray has arr[][][][] resultant size will be n x n x n x n; and so on.
I know I had to implement necessary methods as I add a new dimension to the base array.
But I would like a review on this type of storage class. I don't know
what's the maximum dimensions java array can have?
Also, do tell me
- if can use this approach to create arrays size more than the traditional array size
- Future scope such as generic BigArray of character will create string greater size
- other opinions/suggestions on this approach
java array
$endgroup$
add a comment
|
$begingroup$
I was solving one competitive coding question having integer bounds 2^100. Fortunately, it was a dp question and I didn't need an array of that size. Practically we cannot create an array of size more than Integer.MAX_VALUE.
But I was thinking of creating a multidimensional array and treat it as a one-dimensional array.
what I need was a very large array with O(1) retrieval time. So Internally it will be a multidimensional array but I will treat it as a one-dimensional array.
So the size of the resultant array will be n*n.
if n is Integer.MAX_VALUE then the resultant augmented array will be of Integer.MAX * Integer.MAX
Here what I did to implement this idea.
BigArray class
public class BigArray
private int[][] arr;
private int row=0;
private int col = 0;
private int size;
public BigArray(int size)
arr = new int[size][size];
this.size = size;
add method
public void add(int data)
if(row > size-1)
col++;
row=0;
this.arr[col][row] = data;
row++;
else
this.arr[col][row] = data;
row++;
get method
public int get(int pos)
//get value in O(1)
if((int) Math.sqrt(pos) > size)
return -1;
int col1 = pos/(size);
int row1 = pos%(size);
return arr[col1][row1];
But If I pass Integer.MAX_VALUE in BigArray it throws Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
maximum size I've tried is 20,000 so resultant size will be 20,000 x 20,000 = 400,000,000.
But if add one more dimension to this array I'll get more space.
if BigArray has arr[][][] resultant size will be n x n x n;
if BigArray has arr[][][][] resultant size will be n x n x n x n; and so on.
I know I had to implement necessary methods as I add a new dimension to the base array.
But I would like a review on this type of storage class. I don't know
what's the maximum dimensions java array can have?
Also, do tell me
- if can use this approach to create arrays size more than the traditional array size
- Future scope such as generic BigArray of character will create string greater size
- other opinions/suggestions on this approach
java array
$endgroup$
$begingroup$
How big of an array are you actually aiming for? Multidimensional with Integer.MAX_VALUE each dimension would be huge
$endgroup$
– D. Jurcau
6 hours ago
$begingroup$
Yes Im trying create array of size larger than Integer.MAX
$endgroup$
– Aashish Pawar
6 hours ago
add a comment
|
$begingroup$
I was solving one competitive coding question having integer bounds 2^100. Fortunately, it was a dp question and I didn't need an array of that size. Practically we cannot create an array of size more than Integer.MAX_VALUE.
But I was thinking of creating a multidimensional array and treat it as a one-dimensional array.
what I need was a very large array with O(1) retrieval time. So Internally it will be a multidimensional array but I will treat it as a one-dimensional array.
So the size of the resultant array will be n*n.
if n is Integer.MAX_VALUE then the resultant augmented array will be of Integer.MAX * Integer.MAX
Here what I did to implement this idea.
BigArray class
public class BigArray
private int[][] arr;
private int row=0;
private int col = 0;
private int size;
public BigArray(int size)
arr = new int[size][size];
this.size = size;
add method
public void add(int data)
if(row > size-1)
col++;
row=0;
this.arr[col][row] = data;
row++;
else
this.arr[col][row] = data;
row++;
get method
public int get(int pos)
//get value in O(1)
if((int) Math.sqrt(pos) > size)
return -1;
int col1 = pos/(size);
int row1 = pos%(size);
return arr[col1][row1];
But If I pass Integer.MAX_VALUE in BigArray it throws Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
maximum size I've tried is 20,000 so resultant size will be 20,000 x 20,000 = 400,000,000.
But if add one more dimension to this array I'll get more space.
if BigArray has arr[][][] resultant size will be n x n x n;
if BigArray has arr[][][][] resultant size will be n x n x n x n; and so on.
I know I had to implement necessary methods as I add a new dimension to the base array.
But I would like a review on this type of storage class. I don't know
what's the maximum dimensions java array can have?
Also, do tell me
- if can use this approach to create arrays size more than the traditional array size
- Future scope such as generic BigArray of character will create string greater size
- other opinions/suggestions on this approach
java array
$endgroup$
I was solving one competitive coding question having integer bounds 2^100. Fortunately, it was a dp question and I didn't need an array of that size. Practically we cannot create an array of size more than Integer.MAX_VALUE.
But I was thinking of creating a multidimensional array and treat it as a one-dimensional array.
what I need was a very large array with O(1) retrieval time. So Internally it will be a multidimensional array but I will treat it as a one-dimensional array.
So the size of the resultant array will be n*n.
if n is Integer.MAX_VALUE then the resultant augmented array will be of Integer.MAX * Integer.MAX
Here what I did to implement this idea.
BigArray class
public class BigArray
private int[][] arr;
private int row=0;
private int col = 0;
private int size;
public BigArray(int size)
arr = new int[size][size];
this.size = size;
add method
public void add(int data)
if(row > size-1)
col++;
row=0;
this.arr[col][row] = data;
row++;
else
this.arr[col][row] = data;
row++;
get method
public int get(int pos)
//get value in O(1)
if((int) Math.sqrt(pos) > size)
return -1;
int col1 = pos/(size);
int row1 = pos%(size);
return arr[col1][row1];
But If I pass Integer.MAX_VALUE in BigArray it throws Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
maximum size I've tried is 20,000 so resultant size will be 20,000 x 20,000 = 400,000,000.
But if add one more dimension to this array I'll get more space.
if BigArray has arr[][][] resultant size will be n x n x n;
if BigArray has arr[][][][] resultant size will be n x n x n x n; and so on.
I know I had to implement necessary methods as I add a new dimension to the base array.
But I would like a review on this type of storage class. I don't know
what's the maximum dimensions java array can have?
Also, do tell me
- if can use this approach to create arrays size more than the traditional array size
- Future scope such as generic BigArray of character will create string greater size
- other opinions/suggestions on this approach
java array
java array
edited 7 hours ago
AJNeufeld
12.7k1 gold badge13 silver badges40 bronze badges
12.7k1 gold badge13 silver badges40 bronze badges
asked 8 hours ago
Aashish PawarAashish Pawar
486 bronze badges
486 bronze badges
$begingroup$
How big of an array are you actually aiming for? Multidimensional with Integer.MAX_VALUE each dimension would be huge
$endgroup$
– D. Jurcau
6 hours ago
$begingroup$
Yes Im trying create array of size larger than Integer.MAX
$endgroup$
– Aashish Pawar
6 hours ago
add a comment
|
$begingroup$
How big of an array are you actually aiming for? Multidimensional with Integer.MAX_VALUE each dimension would be huge
$endgroup$
– D. Jurcau
6 hours ago
$begingroup$
Yes Im trying create array of size larger than Integer.MAX
$endgroup$
– Aashish Pawar
6 hours ago
$begingroup$
How big of an array are you actually aiming for? Multidimensional with Integer.MAX_VALUE each dimension would be huge
$endgroup$
– D. Jurcau
6 hours ago
$begingroup$
How big of an array are you actually aiming for? Multidimensional with Integer.MAX_VALUE each dimension would be huge
$endgroup$
– D. Jurcau
6 hours ago
$begingroup$
Yes Im trying create array of size larger than Integer.MAX
$endgroup$
– Aashish Pawar
6 hours ago
$begingroup$
Yes Im trying create array of size larger than Integer.MAX
$endgroup$
– Aashish Pawar
6 hours ago
add a comment
|
3 Answers
3
active
oldest
votes
$begingroup$
The biggest problem I can see with this approach, is that a user can't add or get any index higher than Integer.MAXVALUE. You might need to parse Strings or use a BigInteger type instead of int for those methods.
$endgroup$
add a comment
|
$begingroup$
Have you increased the size of your VM? If not, you will not get past java.lang.OutOfMemoryError: Requested array size exceeds VM limit without doing something creative like storing the array in a file.
Do you need 4-byte integers, or would 2-byte shorts be sufficient?
This is allocating all the memory in one chunk:
arr = new int[size][size];
Perhaps you should use:
arr = new int[size][];
arr[0] = new int[size];
to allocate one chunk to hold the columns, one chunk to hold the first column, and then as data is being added, allocate new columns on demand:
public void add(int data)
if (row >= size)
arr[++col] = new int[size];
row = 0;
arr[col][row] = data;
row++;
Using a variable size, or even Integer.MAX_VALUE to partition data into rows, columns, and higher dimensions is inefficient. I’d use a hard-coded power of 2, to allow efficient module arithmetic.
$endgroup$
add a comment
|
$begingroup$
In addition to the answer @tinstaafl provided, I think that your class doesn't behave like arrays are implemented: namely, it is allocated in different places in the program's memory space. You can't, for instance, call System.arraycopy() to make a copy out of it.
I think that you implement here a specific Map, with keys as integers (or longs, as mentioned), and values as integers.
$endgroup$
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f230223%2fjava-creating-augmented-array-of-size-400-000-000%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
The biggest problem I can see with this approach, is that a user can't add or get any index higher than Integer.MAXVALUE. You might need to parse Strings or use a BigInteger type instead of int for those methods.
$endgroup$
add a comment
|
$begingroup$
The biggest problem I can see with this approach, is that a user can't add or get any index higher than Integer.MAXVALUE. You might need to parse Strings or use a BigInteger type instead of int for those methods.
$endgroup$
add a comment
|
$begingroup$
The biggest problem I can see with this approach, is that a user can't add or get any index higher than Integer.MAXVALUE. You might need to parse Strings or use a BigInteger type instead of int for those methods.
$endgroup$
The biggest problem I can see with this approach, is that a user can't add or get any index higher than Integer.MAXVALUE. You might need to parse Strings or use a BigInteger type instead of int for those methods.
answered 5 hours ago
tinstaafltinstaafl
7,7471 gold badge9 silver badges31 bronze badges
7,7471 gold badge9 silver badges31 bronze badges
add a comment
|
add a comment
|
$begingroup$
Have you increased the size of your VM? If not, you will not get past java.lang.OutOfMemoryError: Requested array size exceeds VM limit without doing something creative like storing the array in a file.
Do you need 4-byte integers, or would 2-byte shorts be sufficient?
This is allocating all the memory in one chunk:
arr = new int[size][size];
Perhaps you should use:
arr = new int[size][];
arr[0] = new int[size];
to allocate one chunk to hold the columns, one chunk to hold the first column, and then as data is being added, allocate new columns on demand:
public void add(int data)
if (row >= size)
arr[++col] = new int[size];
row = 0;
arr[col][row] = data;
row++;
Using a variable size, or even Integer.MAX_VALUE to partition data into rows, columns, and higher dimensions is inefficient. I’d use a hard-coded power of 2, to allow efficient module arithmetic.
$endgroup$
add a comment
|
$begingroup$
Have you increased the size of your VM? If not, you will not get past java.lang.OutOfMemoryError: Requested array size exceeds VM limit without doing something creative like storing the array in a file.
Do you need 4-byte integers, or would 2-byte shorts be sufficient?
This is allocating all the memory in one chunk:
arr = new int[size][size];
Perhaps you should use:
arr = new int[size][];
arr[0] = new int[size];
to allocate one chunk to hold the columns, one chunk to hold the first column, and then as data is being added, allocate new columns on demand:
public void add(int data)
if (row >= size)
arr[++col] = new int[size];
row = 0;
arr[col][row] = data;
row++;
Using a variable size, or even Integer.MAX_VALUE to partition data into rows, columns, and higher dimensions is inefficient. I’d use a hard-coded power of 2, to allow efficient module arithmetic.
$endgroup$
add a comment
|
$begingroup$
Have you increased the size of your VM? If not, you will not get past java.lang.OutOfMemoryError: Requested array size exceeds VM limit without doing something creative like storing the array in a file.
Do you need 4-byte integers, or would 2-byte shorts be sufficient?
This is allocating all the memory in one chunk:
arr = new int[size][size];
Perhaps you should use:
arr = new int[size][];
arr[0] = new int[size];
to allocate one chunk to hold the columns, one chunk to hold the first column, and then as data is being added, allocate new columns on demand:
public void add(int data)
if (row >= size)
arr[++col] = new int[size];
row = 0;
arr[col][row] = data;
row++;
Using a variable size, or even Integer.MAX_VALUE to partition data into rows, columns, and higher dimensions is inefficient. I’d use a hard-coded power of 2, to allow efficient module arithmetic.
$endgroup$
Have you increased the size of your VM? If not, you will not get past java.lang.OutOfMemoryError: Requested array size exceeds VM limit without doing something creative like storing the array in a file.
Do you need 4-byte integers, or would 2-byte shorts be sufficient?
This is allocating all the memory in one chunk:
arr = new int[size][size];
Perhaps you should use:
arr = new int[size][];
arr[0] = new int[size];
to allocate one chunk to hold the columns, one chunk to hold the first column, and then as data is being added, allocate new columns on demand:
public void add(int data)
if (row >= size)
arr[++col] = new int[size];
row = 0;
arr[col][row] = data;
row++;
Using a variable size, or even Integer.MAX_VALUE to partition data into rows, columns, and higher dimensions is inefficient. I’d use a hard-coded power of 2, to allow efficient module arithmetic.
answered 3 hours ago
AJNeufeldAJNeufeld
12.7k1 gold badge13 silver badges40 bronze badges
12.7k1 gold badge13 silver badges40 bronze badges
add a comment
|
add a comment
|
$begingroup$
In addition to the answer @tinstaafl provided, I think that your class doesn't behave like arrays are implemented: namely, it is allocated in different places in the program's memory space. You can't, for instance, call System.arraycopy() to make a copy out of it.
I think that you implement here a specific Map, with keys as integers (or longs, as mentioned), and values as integers.
$endgroup$
add a comment
|
$begingroup$
In addition to the answer @tinstaafl provided, I think that your class doesn't behave like arrays are implemented: namely, it is allocated in different places in the program's memory space. You can't, for instance, call System.arraycopy() to make a copy out of it.
I think that you implement here a specific Map, with keys as integers (or longs, as mentioned), and values as integers.
$endgroup$
add a comment
|
$begingroup$
In addition to the answer @tinstaafl provided, I think that your class doesn't behave like arrays are implemented: namely, it is allocated in different places in the program's memory space. You can't, for instance, call System.arraycopy() to make a copy out of it.
I think that you implement here a specific Map, with keys as integers (or longs, as mentioned), and values as integers.
$endgroup$
In addition to the answer @tinstaafl provided, I think that your class doesn't behave like arrays are implemented: namely, it is allocated in different places in the program's memory space. You can't, for instance, call System.arraycopy() to make a copy out of it.
I think that you implement here a specific Map, with keys as integers (or longs, as mentioned), and values as integers.
answered 3 hours ago
Ron KleinRon Klein
7877 silver badges16 bronze badges
7877 silver badges16 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f230223%2fjava-creating-augmented-array-of-size-400-000-000%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
How big of an array are you actually aiming for? Multidimensional with Integer.MAX_VALUE each dimension would be huge
$endgroup$
– D. Jurcau
6 hours ago
$begingroup$
Yes Im trying create array of size larger than Integer.MAX
$endgroup$
– Aashish Pawar
6 hours ago