当前位置:   article > 正文

[Jvava] List的add底层实现原理_list保留数字add

list保留数字add

list的add方法底层实现原理是基于指针,list对象指向添加元素的内存地址,对应内存地址元素发生改变,它的值也会发生改变。
在下面的代码中:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList();
        if( root == null ) return res;
        List<Integer> path = new ArrayList();
        getSum(root, targetSum, res, path, 0);
        return res;

    }

    public void getSum(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path, int sum) {
        sum += root.val;
        path.add(root.val);
        if( (root.left == null) && (root.right == null) ) {

            if( sum == targetSum ) {

                // System.out.println(path.toString());
                // System.out.println(path.size());
                // List<Integer> tmp = new ArrayList();
                // for(Integer p:path) {
                //    tmp.add(p);
                // }
                res.add(path);

            } 
            return;
            // path.remove(path.size()-1);
        }
        if(root.left != null ) {
            getSum(root.left, targetSum, res, path, sum);
            path.remove(path.size()-1);
        }
        if(root.right != null ) {
            getSum(root.right, targetSum, res, path, sum);
            path.remove(path.size()-1);
        }
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

path发生改变,对应的res也会改变。
而下面的代码中,res会保留原本值,根本原因是,用了新的内存来保存相应的值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList();
        if( root == null ) return res;
        List<Integer> path = new ArrayList();
        getSum(root, targetSum, res, path, 0);
        return res;

    }

    public void getSum(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path, int sum) {
        sum += root.val;
        path.add(root.val);
        if( (root.left == null) && (root.right == null) ) {

            if( sum == targetSum ) {

                // System.out.println(path.toString());
                // System.out.println(path.size());
                List<Integer> tmp = new ArrayList();
                for(Integer p:path) {
                    tmp.add(p);
                }
                res.add(tmp);

            } 
            return;
            // path.remove(path.size()-1);
        }
        if(root.left != null ) {
            getSum(root.left, targetSum, res, path, sum);
            path.remove(path.size()-1);
        }
        if(root.right != null ) {
            getSum(root.right, targetSum, res, path, sum);
            path.remove(path.size()-1);
        }
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/思考机器3/article/detail/62891
推荐阅读
相关标签
  

闽ICP备14008679号