ArrayList(lintcode 385)

Description

Implement an ArrayListManager which can:

  1. create(n). Create an ArrayList of integers contains [0, 1, 2, ... n-1]
  2. clone(list). Clone a list. The cloned list should independent with the original list.
  3. get(list, index). Get the element on the index position of the list.
  4. set(list, index, val). Change the value the element of index position to given val.
  5. remove(list, index). Remove the element on the index position.
  6. indexOf(list, val). Find the first index of element that equals to val and return its index.

Please use the methods provided by ArrayList. See documents:ArrayList Document

Example

ArrayList<Integer> list = ArrayListManager.create(5);
list.get(0);  // should return 0
list.get(1);  // should return 1
list.get(4);  // should return 4

// clone_list should be [0,1,2,3,4]
ArrayList<Integer> clone_list = ArrayListManager.clone(list); 
ArrayListManager.get(list, 2);  // should return 2
ArrayListManager.indexOf(list, 1); // should return 1
ArrayListManager.indexOf(list, 10); // should return -1
ArrayListManager.remove(list, 3); // list will become [0, 1, 2, 4]
ArrayListManager.get(list, 3); // as 3 has been removed, should return 4
ArrayListManager.set(list, 2, 3); // list will become [0, 1, 3, 4]

clone_list.get(2); // should return 2
clone_list.get(3); // should return 3

Interface

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) {
        // Write your code here
    }

    /**
     * @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) {
        // Write your code here
    }

    /**
     * @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) {
        // Write your code here
    }

    /**
     * @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) {
        // write your code here
    }

    /**
     * @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) {
        // write tour code here
    }

    /**
     * @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) {
        // Write your code here
    }
}

Idea

Most of the functions can be realized by the methods in ArrayList Class.

Solution

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) {
        // Write your code here
        ArrayList<Integer> res = new ArrayList<Integer>();
        for (int i = 0; i < n; ++i) {
            res.add(i);
        }
        return res;
    }

    /**
     * @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) {
        // Write your code here
        ArrayList<Integer> res = new ArrayList<Integer>();
        for (Integer num : list) {
            res.add(num);
        }
        return res;
    }

    /**
     * @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) {
        // Write your code here
        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) {
        // write your code here
        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) {
        // write tour code here
        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) {
        // Write your code here
        return list.indexOf(val);
    }
}

results matching ""

    No results matching ""