0385 0456

作者: 日光降临 | 来源:发表于2019-02-09 00:24 被阅读0次
public class ArrayListManager {
    /**
     * @param n: You should generate an array list of n elements.
     * @return: The array list your just created.
     */
    public static ArrayList<Integer> create(int n) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        int i;
        for(i=0;i<n;i++)
            list.add(i);
        return list;
    }
    
    /**
     * @param list: The list you need to clone
     * @return: A deep copyed array list from the given list
     */
    public static ArrayList<Integer> clone(ArrayList<Integer> list) {
        ArrayList<Integer> dist = new ArrayList<Integer>();
        for(Integer a : list)
            dist.add(a);
        return dist;
    }
    
    /**
     * @param list: The array list to find the kth element
     * @param k: Find the kth element
     * @return: The kth element
     */
    public static int get(ArrayList<Integer> list, int k) {
        return list.get(k);
    }
    
    /**
     * @param list: The array list
     * @param k: Find the kth element, set it to val
     * @param val: Find the kth element, set it to val
     */
    public static void set(ArrayList<Integer> list, int k, int val) {
        list.set(k,val);
    }
    
    /**
     * @param list: The array list to remove the kth element
     * @param k: Remove the kth element
     */
    public static void remove(ArrayList<Integer> list, int k) {
        list.remove(k);
    }
    
    /**
     * @param list: The array list.
     * @param val: Get the index of the first element that equals to val
     * @return: Return the index of that element
     */
    public static int indexOf(ArrayList<Integer> list, int val) {
        if(list==null)
            return -1;
        return list.indexOf(val);
    }
}
/**
 * Definition of Node:
 * class Node {
 *     public int val;
 *     public Node(int val) {
 *         this.val = val;
 *     }
 * }
 */
public class ReferenceManager {
    public Node node;

    public void copyValue(Node obj) {
        if(obj==null)
            return;
        if(node==null){
            node = new Node(obj.val);
            return;
        }
        this.node.val = obj.val;
    }

    public void copyReference(Node obj) {
        if(obj!=null)
            this.node = obj;
    }
}

相关文章

网友评论

      本文标题:0385 0456

      本文链接:https://www.haomeiwen.com/subject/oyhasqtx.html