Dynamically creating a 2D array in Groovy

Multi tool use
I have already gone through all the questions on stackoverflow regarding Groovy 2D arrays and the answers do not seem to suffice. I am also fairly new to groovy hence, I'd sincerely appreciate your input in this regard.
I am trying to create a 2D array in the following way.
def extractedArray =
// fullArray is a 2D array of size[11987][4]
def x = 0
for (x; x<fullArray.length; x++)
if (x==0)
extractedArray =
extractedArray[0][0]=fullArray[0][0]
extractedArray[0][1]=fullArray[0][2]
else
for(def y=0; y<extractedArray.length; y++)
//other functions
But this seems to give me the following error.
Caught: java.lang.NullPointerException: Cannot invoke method putAt() on null object
java.lang.NullPointerException: Cannot invoke method putAt() on null object
at computeMPR.main(computeMPR.groovy:37)
arrays dynamic groovy 2d
add a comment |
I have already gone through all the questions on stackoverflow regarding Groovy 2D arrays and the answers do not seem to suffice. I am also fairly new to groovy hence, I'd sincerely appreciate your input in this regard.
I am trying to create a 2D array in the following way.
def extractedArray =
// fullArray is a 2D array of size[11987][4]
def x = 0
for (x; x<fullArray.length; x++)
if (x==0)
extractedArray =
extractedArray[0][0]=fullArray[0][0]
extractedArray[0][1]=fullArray[0][2]
else
for(def y=0; y<extractedArray.length; y++)
//other functions
But this seems to give me the following error.
Caught: java.lang.NullPointerException: Cannot invoke method putAt() on null object
java.lang.NullPointerException: Cannot invoke method putAt() on null object
at computeMPR.main(computeMPR.groovy:37)
arrays dynamic groovy 2d
add a comment |
I have already gone through all the questions on stackoverflow regarding Groovy 2D arrays and the answers do not seem to suffice. I am also fairly new to groovy hence, I'd sincerely appreciate your input in this regard.
I am trying to create a 2D array in the following way.
def extractedArray =
// fullArray is a 2D array of size[11987][4]
def x = 0
for (x; x<fullArray.length; x++)
if (x==0)
extractedArray =
extractedArray[0][0]=fullArray[0][0]
extractedArray[0][1]=fullArray[0][2]
else
for(def y=0; y<extractedArray.length; y++)
//other functions
But this seems to give me the following error.
Caught: java.lang.NullPointerException: Cannot invoke method putAt() on null object
java.lang.NullPointerException: Cannot invoke method putAt() on null object
at computeMPR.main(computeMPR.groovy:37)
arrays dynamic groovy 2d
I have already gone through all the questions on stackoverflow regarding Groovy 2D arrays and the answers do not seem to suffice. I am also fairly new to groovy hence, I'd sincerely appreciate your input in this regard.
I am trying to create a 2D array in the following way.
def extractedArray =
// fullArray is a 2D array of size[11987][4]
def x = 0
for (x; x<fullArray.length; x++)
if (x==0)
extractedArray =
extractedArray[0][0]=fullArray[0][0]
extractedArray[0][1]=fullArray[0][2]
else
for(def y=0; y<extractedArray.length; y++)
//other functions
But this seems to give me the following error.
Caught: java.lang.NullPointerException: Cannot invoke method putAt() on null object
java.lang.NullPointerException: Cannot invoke method putAt() on null object
at computeMPR.main(computeMPR.groovy:37)
arrays dynamic groovy 2d
arrays dynamic groovy 2d
edited Nov 16 '18 at 6:54


Kartik
4,45731537
4,45731537
asked Nov 16 '18 at 6:51


Nayantara JeyarajNayantara Jeyaraj
1,77541742
1,77541742
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
def extractedArray =
this is not array but List initialization
so you can change code:
//extractedArray[0][0]=fullArray[0][0]
//extractedArray[0][1]=fullArray[0][2]
extractedArray << [fullArray[0][0], fullArray[0][2]]
or if you prefer arrays you can do something like:
Object extractedArray = [fullArray.size()]
extractedArray[0] = [
fullArray[0][0],
fullArray[0][2]
] as Object
Thanks for your response. But this is the problem. WithObject extractedArray = [fullArray.size()]
, we're creating theextractedArray
with the same size as thefullArray
right? But, in my case, we don't know the size of theextractedArray
. I simply want to define the array without specifying a size. Then as we push the values to the array, it dynamically increases in size.
– Nayantara Jeyaraj
Nov 16 '18 at 10:24
Since we initially don't know how many elements we'll be storing in the array, the array needs to dynamically grow as the elements are inserted. I know how to do this with the other scripting languages like JS, but not exactly sure about groovy. Any suggestions? Thanks.
– Nayantara Jeyaraj
Nov 16 '18 at 10:26
You can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size.
– Evgeny Smirnov
Nov 16 '18 at 10:53
But this is possible with Javascript right? We simple declare an array and then as wearray.push('x')
elements to it, the array size increases as well. Isn't the same possible with groovy?
– Nayantara Jeyaraj
Nov 19 '18 at 3:40
Groovy is based on Java. In java there are no way to create array with dynamic size. Collections are developed to solve this problem. List is much more powerful structure then regular array. And if you need array on the end you can simply transform list to array: list.toArray(new Object[list.size()])
– Evgeny Smirnov
Nov 19 '18 at 6:42
|
show 1 more 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: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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/3.0/"u003ecc by-sa 3.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%2fstackoverflow.com%2fquestions%2f53332804%2fdynamically-creating-a-2d-array-in-groovy%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
def extractedArray =
this is not array but List initialization
so you can change code:
//extractedArray[0][0]=fullArray[0][0]
//extractedArray[0][1]=fullArray[0][2]
extractedArray << [fullArray[0][0], fullArray[0][2]]
or if you prefer arrays you can do something like:
Object extractedArray = [fullArray.size()]
extractedArray[0] = [
fullArray[0][0],
fullArray[0][2]
] as Object
Thanks for your response. But this is the problem. WithObject extractedArray = [fullArray.size()]
, we're creating theextractedArray
with the same size as thefullArray
right? But, in my case, we don't know the size of theextractedArray
. I simply want to define the array without specifying a size. Then as we push the values to the array, it dynamically increases in size.
– Nayantara Jeyaraj
Nov 16 '18 at 10:24
Since we initially don't know how many elements we'll be storing in the array, the array needs to dynamically grow as the elements are inserted. I know how to do this with the other scripting languages like JS, but not exactly sure about groovy. Any suggestions? Thanks.
– Nayantara Jeyaraj
Nov 16 '18 at 10:26
You can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size.
– Evgeny Smirnov
Nov 16 '18 at 10:53
But this is possible with Javascript right? We simple declare an array and then as wearray.push('x')
elements to it, the array size increases as well. Isn't the same possible with groovy?
– Nayantara Jeyaraj
Nov 19 '18 at 3:40
Groovy is based on Java. In java there are no way to create array with dynamic size. Collections are developed to solve this problem. List is much more powerful structure then regular array. And if you need array on the end you can simply transform list to array: list.toArray(new Object[list.size()])
– Evgeny Smirnov
Nov 19 '18 at 6:42
|
show 1 more comment
def extractedArray =
this is not array but List initialization
so you can change code:
//extractedArray[0][0]=fullArray[0][0]
//extractedArray[0][1]=fullArray[0][2]
extractedArray << [fullArray[0][0], fullArray[0][2]]
or if you prefer arrays you can do something like:
Object extractedArray = [fullArray.size()]
extractedArray[0] = [
fullArray[0][0],
fullArray[0][2]
] as Object
Thanks for your response. But this is the problem. WithObject extractedArray = [fullArray.size()]
, we're creating theextractedArray
with the same size as thefullArray
right? But, in my case, we don't know the size of theextractedArray
. I simply want to define the array without specifying a size. Then as we push the values to the array, it dynamically increases in size.
– Nayantara Jeyaraj
Nov 16 '18 at 10:24
Since we initially don't know how many elements we'll be storing in the array, the array needs to dynamically grow as the elements are inserted. I know how to do this with the other scripting languages like JS, but not exactly sure about groovy. Any suggestions? Thanks.
– Nayantara Jeyaraj
Nov 16 '18 at 10:26
You can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size.
– Evgeny Smirnov
Nov 16 '18 at 10:53
But this is possible with Javascript right? We simple declare an array and then as wearray.push('x')
elements to it, the array size increases as well. Isn't the same possible with groovy?
– Nayantara Jeyaraj
Nov 19 '18 at 3:40
Groovy is based on Java. In java there are no way to create array with dynamic size. Collections are developed to solve this problem. List is much more powerful structure then regular array. And if you need array on the end you can simply transform list to array: list.toArray(new Object[list.size()])
– Evgeny Smirnov
Nov 19 '18 at 6:42
|
show 1 more comment
def extractedArray =
this is not array but List initialization
so you can change code:
//extractedArray[0][0]=fullArray[0][0]
//extractedArray[0][1]=fullArray[0][2]
extractedArray << [fullArray[0][0], fullArray[0][2]]
or if you prefer arrays you can do something like:
Object extractedArray = [fullArray.size()]
extractedArray[0] = [
fullArray[0][0],
fullArray[0][2]
] as Object
def extractedArray =
this is not array but List initialization
so you can change code:
//extractedArray[0][0]=fullArray[0][0]
//extractedArray[0][1]=fullArray[0][2]
extractedArray << [fullArray[0][0], fullArray[0][2]]
or if you prefer arrays you can do something like:
Object extractedArray = [fullArray.size()]
extractedArray[0] = [
fullArray[0][0],
fullArray[0][2]
] as Object
answered Nov 16 '18 at 7:38


Evgeny SmirnovEvgeny Smirnov
2,0811618
2,0811618
Thanks for your response. But this is the problem. WithObject extractedArray = [fullArray.size()]
, we're creating theextractedArray
with the same size as thefullArray
right? But, in my case, we don't know the size of theextractedArray
. I simply want to define the array without specifying a size. Then as we push the values to the array, it dynamically increases in size.
– Nayantara Jeyaraj
Nov 16 '18 at 10:24
Since we initially don't know how many elements we'll be storing in the array, the array needs to dynamically grow as the elements are inserted. I know how to do this with the other scripting languages like JS, but not exactly sure about groovy. Any suggestions? Thanks.
– Nayantara Jeyaraj
Nov 16 '18 at 10:26
You can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size.
– Evgeny Smirnov
Nov 16 '18 at 10:53
But this is possible with Javascript right? We simple declare an array and then as wearray.push('x')
elements to it, the array size increases as well. Isn't the same possible with groovy?
– Nayantara Jeyaraj
Nov 19 '18 at 3:40
Groovy is based on Java. In java there are no way to create array with dynamic size. Collections are developed to solve this problem. List is much more powerful structure then regular array. And if you need array on the end you can simply transform list to array: list.toArray(new Object[list.size()])
– Evgeny Smirnov
Nov 19 '18 at 6:42
|
show 1 more comment
Thanks for your response. But this is the problem. WithObject extractedArray = [fullArray.size()]
, we're creating theextractedArray
with the same size as thefullArray
right? But, in my case, we don't know the size of theextractedArray
. I simply want to define the array without specifying a size. Then as we push the values to the array, it dynamically increases in size.
– Nayantara Jeyaraj
Nov 16 '18 at 10:24
Since we initially don't know how many elements we'll be storing in the array, the array needs to dynamically grow as the elements are inserted. I know how to do this with the other scripting languages like JS, but not exactly sure about groovy. Any suggestions? Thanks.
– Nayantara Jeyaraj
Nov 16 '18 at 10:26
You can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size.
– Evgeny Smirnov
Nov 16 '18 at 10:53
But this is possible with Javascript right? We simple declare an array and then as wearray.push('x')
elements to it, the array size increases as well. Isn't the same possible with groovy?
– Nayantara Jeyaraj
Nov 19 '18 at 3:40
Groovy is based on Java. In java there are no way to create array with dynamic size. Collections are developed to solve this problem. List is much more powerful structure then regular array. And if you need array on the end you can simply transform list to array: list.toArray(new Object[list.size()])
– Evgeny Smirnov
Nov 19 '18 at 6:42
Thanks for your response. But this is the problem. With
Object extractedArray = [fullArray.size()]
, we're creating the extractedArray
with the same size as the fullArray
right? But, in my case, we don't know the size of the extractedArray
. I simply want to define the array without specifying a size. Then as we push the values to the array, it dynamically increases in size.– Nayantara Jeyaraj
Nov 16 '18 at 10:24
Thanks for your response. But this is the problem. With
Object extractedArray = [fullArray.size()]
, we're creating the extractedArray
with the same size as the fullArray
right? But, in my case, we don't know the size of the extractedArray
. I simply want to define the array without specifying a size. Then as we push the values to the array, it dynamically increases in size.– Nayantara Jeyaraj
Nov 16 '18 at 10:24
Since we initially don't know how many elements we'll be storing in the array, the array needs to dynamically grow as the elements are inserted. I know how to do this with the other scripting languages like JS, but not exactly sure about groovy. Any suggestions? Thanks.
– Nayantara Jeyaraj
Nov 16 '18 at 10:26
Since we initially don't know how many elements we'll be storing in the array, the array needs to dynamically grow as the elements are inserted. I know how to do this with the other scripting languages like JS, but not exactly sure about groovy. Any suggestions? Thanks.
– Nayantara Jeyaraj
Nov 16 '18 at 10:26
You can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size.
– Evgeny Smirnov
Nov 16 '18 at 10:53
You can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size.
– Evgeny Smirnov
Nov 16 '18 at 10:53
But this is possible with Javascript right? We simple declare an array and then as we
array.push('x')
elements to it, the array size increases as well. Isn't the same possible with groovy?– Nayantara Jeyaraj
Nov 19 '18 at 3:40
But this is possible with Javascript right? We simple declare an array and then as we
array.push('x')
elements to it, the array size increases as well. Isn't the same possible with groovy?– Nayantara Jeyaraj
Nov 19 '18 at 3:40
Groovy is based on Java. In java there are no way to create array with dynamic size. Collections are developed to solve this problem. List is much more powerful structure then regular array. And if you need array on the end you can simply transform list to array: list.toArray(new Object[list.size()])
– Evgeny Smirnov
Nov 19 '18 at 6:42
Groovy is based on Java. In java there are no way to create array with dynamic size. Collections are developed to solve this problem. List is much more powerful structure then regular array. And if you need array on the end you can simply transform list to array: list.toArray(new Object[list.size()])
– Evgeny Smirnov
Nov 19 '18 at 6:42
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f53332804%2fdynamically-creating-a-2d-array-in-groovy%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
KwSTnLHVox8CHA,I8jZ78GT9z1j5 V,KHYN DHLS1 af7,K1a6,nBkj7mBnxvx,2K8 ZiRFfMyxF,waP