class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<>();     
        for(int i=0;i<=nums.length-3;i++){
            if(i==0 || nums[i]!=nums[i-1]){
                int L=i+1;
                int R = nums.length-1;
                int target = 0-(nums[i]);
                while(L<R){ 
                    if(nums[L]+nums[R]==target){
                        List<Integer> temp = new ArrayList<>();
                        temp.add(nums[i]);
                        temp.add(nums[L]);
                        temp.add(nums[R]);
                        ans.add(temp);
                        while(L<nums.length-1 && nums[L]==nums[L+1]) L++;
                        while(R>0 && nums[R]==nums[R-1]) R--;
                        L++;
                        R--;
                    }else if(nums[L]+nums[R]<target){
                        L+=1;
                    }else{
                        R+=1;
                    }
                }    
            }
        }
    return ans;    
    }
# }


trying to solve the 3 sum problem of leetcode with 2 pointers approach . I am getting java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6 at
if(nums[L]+nums[R]==target){

Why does my 2 pointers approach to solve the 3 sum problem on LeetCode throw an index out of bounds error?
A error in the condition for increasing the R pointer is probably what is generating the ArrayIndexOutOfBoundsException in your code. R-=1 should be used instead of R+=1 to move the pointer to the left.
The updated code is as follows:
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0; i <= nums.length - 3; i++) {
            if (i == 0 || nums[i] != nums[i - 1]) {
                int L = i + 1;
                int R = nums.length - 1;
                int target = 0 - nums[i];
                while (L < R) {
                    if (nums[L] + nums[R] == target) {
                        List<Integer> temp = new ArrayList<>();
                        temp.add(nums[i]);
                        temp.add(nums[L]);
                        temp.add(nums[R]);
                        ans.add(temp);
                        while (L < nums.length - 1 && nums[L] == nums[L + 1]) L++;
                        while (R > 0 && nums[R] == nums[R - 1]) R--;
                        L++;
                        R--;
                    } else if (nums[L] + nums[R] < target) {
                        L += 1;
                    } else {
                        R -= 1;  // Fix the typo here
                    }
                }
            }
        }
        return ans;
    }
}

