ArrayIndexOutOfBoundsException occurs when using a large SSJS array

When I tried to compare the difference in runtime between iteration of the forward and reverse loops in server-side JavaScript (SSJS), a (weird) problem arose. Although this code example

var i,n=9999; var arr=new Array(n); for (i=0;i<n;i++) arr[i]=i; // WORKS i=n; while (i--) arr[i]=i; // WORKS 

working fine, the following code

 var i,n=10000; // changed n from 9999 to 10000 var arr=new Array(n); for (i=0;i<n;i++) arr[i]=i; // WORKS i=n; while (i--) arr[i]=i; // THROWS ArrayIndexOutOfBoundsException 

throws an ArrayIndexOutOfBoundsException for the reverse iteration. The inverse while loop works fine as long as the length of the array is less than 10000 . Can someone tell me what is going on here?

+5
source share
2 answers

A fix for this problem was delivered to the XPage Core Runtime and will be part of the next release of XPages. Thank you for bringing him to our attention.

0
source

Not an answer, but as a workaround for such an error, I recommend using java.util.ArrayList .

 var i,n=10000; var arr=new java.util.ArrayList(n); //changed to ArrayList for (i=0;i<n;i++) arr.add(i); // changed to .add(value) i=n; while (i--) arr.set(i, i); // changed to .set(i, value) 
+1
source

Source: https://habr.com/ru/post/1201064/


All Articles