ArrayList

题目:

Implement an ArrayListManager which can:

create(n). Create an ArrayList of integers contains [0, 1, 2, ... n-1]
clone(list). Clone a list. The cloned list should independent with the original list.
get(list, index). Get the element on the index position of the list.
set(list, index, val). Change the value the element of index position to given val.
remove(list, index). Remove the element on the index position.
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

分析:

这题目不错,对理解ArrayList几个method有很大帮助

确定一个很严重的事儿,list arraylist都不能用[]索引,只有数组才可以

关于list和arraylist的区别

List是一个接口,而ListArray是一个类。 
ListArray继承并实现了List。 
所以List不能被构造,但可以向上面那样为List创建一个引用,而ListArray就可以被构造。 

List list = new ArrayList();这句创建了一个ArrayList的对象后把上溯到了List。
此时它是一个List对象了,有些ArrayList有但是List没有的属性和方法,它就不能再用了。 

问题的关键: 
为什么要用 List list = new ArrayList() ,而不用 ArrayList alist = new ArrayList()呢? 
问题就在于List有多个实现类,现在你用的是ArrayList,也许哪一天你需要换成其它的实现类,
如 LinkedList或者Vector等等,这时你只要改变这一行就行了。

解法:

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 (int i = 0; i < list.size(); i++) {
            res.add(list.get(i) );
        }
        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);
        return;
    }

    /**
     * @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);
        return;
    }

    /**
     * @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
        int res = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == val) {
                res = i;
                break;
            }
        }
        return res;
    }
}

results matching ""

    No results matching ""