Why do I get an ArrayIndexOutOfBoundsException in this prime number check?

后端 未结 5 1385
孤独总比滥情好
孤独总比滥情好 2021-01-15 14:40

I was finding out highest prime factor which divides num, as shown in program, there\'s a issue with array and

arr[j] = i;
j++;
Excepti         


        
5条回答
  •  滥情空心
    2021-01-15 15:28

    Arrays in java are not lists: once allocated, your array won't grow magically.

    You created the array with: int arr[] = {j}; thus the array has one cell only.

    You should initialise your array with at least num/2 cells, with something like int arr[] = new int[num/2]; arr[0] = j;

提交回复
热议问题