Please explain if you can.

Can anyone explain why my 0th index of dpList getting overridden to [1,2,3]?

class Solution {
    public List<Integer> largestDivisibleSubset(int[] nums) {
        ArrayList<Integer>[] dpList = new ArrayList[nums.length];
        List<Integer> ansList = new ArrayList<Integer>();
        for(int i = 0; i< nums.length; i++){
            ArrayList<Integer> intlist = new ArrayList<Integer>();
            intlist.add(nums[i]);
            dpList[i] = intlist;
            System.out.println(" i = "+ i + " and dpList["+ i + "] = "+ dpList[i]);
            for(int j = 0; j < i; j++){
                if((nums[i]%nums[j] == 0 || nums[j]%nums[i]==0) && dpList[i].size() <= dpList[j].size()){
                    System.out.println(" j = "+ j + " and dpList[j] = "+ dpList[j]);
                    ArrayList<Integer> tempList = new ArrayList<Integer>();
                    tempList = dpList[j];
                    tempList.add(nums[i]);
                    System.out.println("tempList "+tempList);
                    dpList[i] = tempList;
                }
                
            }
            
        }
        
        for(int i = 0; i< dpList.length; i++){
            System.out.println("-----> "+ i + " "+ dpList[i]);
            if(dpList[i].size()>ansList.size()){
                ansList = dpList[i];
            }
        }
        return ansList;
    }
}

input : [1, 2, 3]
Output:
i = 0 and dpList[0] = [1]
i = 1 and dpList[1] = [2]
j = 0 and dpList[j] = [1]
tempList [1, 2]
i = 2 and dpList[2] = [3]
j = 0 and dpList[j] = [1, 2]
tempList [1, 2, 3]
-----> 0 [1, 2, 3]
-----> 1 [1, 2, 3]
-----> 2 [1, 2, 3]

Comments (1)