【Lintcode】385. ArrayList

题目地址:

https://www.lintcode.com/problem/arraylist/description

实现ArrayListManager的类。代码如下:

import java.util.ArrayList;

public class ArrayListManager {
    
    /**
     * @param n: You should generate an array list of n elements.
     * @return: The array list your just created.
     */
    public ArrayList<Integer> create(int n) {
        // Write your code here
        ArrayList<Integer> list = new ArrayList<>();
        for (int 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 ArrayList<Integer> clone(ArrayList<Integer> list) {
        // Write your code here
        return new ArrayList<>(list);
    }
    
    /**
     * @param list: The array list to find the kth element
     * @param k:    Find the kth element
     * @return: The kth element
     */
    public 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 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 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 int indexOf(ArrayList<Integer> list, int val) {
        // Write your code here
        return list.indexOf(val);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值