0

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++;
    }
3
  • 1
    How is pos defined? what is the content of token Commented Feb 25, 2015 at 7:00
  • String pos[]=null; token = word/s Commented Feb 25, 2015 at 7:05
  • Show the content of list token. Then only we can solve your java.lang.ArrayIndexOutOfBoundsException Commented Feb 25, 2015 at 7:08

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Now edited it, error is: java.lang.ArrayIndexOutOfBoundsException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.