Reputation: 113
I want to transfer value of even indexed array(pos) to an arraylist(words), everytime pos has two or more value i get OutOfMemoryError. Is something wrong? Because I get OutOfMemoryError: Java heap space
ArrayList<String> token = new ArrayList<String>();
ArrayList<String> words = new ArrayList<String>();
for (String a : token)
{
temp = temp + " " + a;
pos = a.split("[_\\s]+");
}
int c=0;
for(int i=0; i<=pos.length; i+=2) {
words.add(pos[i]);
c++;
}
Upvotes: 0
Views: 1014
Reputation: 3035
In your second for
loop you don't increment i by 2, you set i to 2, resulting in an infinite loop.
use i+=2
instead of i=+2
There is another problem with that for
loop: it can occur that i == pos.length
. In that case pos[i]
will cause an ArrayIndexOutOfBoundsException.
Upvotes: 2