Generate ArrayList with Given Size

题目:

Generate an arrayList with given size, initialize the array list with numbers from 1 to size.

分析:

解法:

public class Solution {
    /**
     * @param size an integer
     * @return an array list
     */
    public ArrayList<Integer> generate(int size) {
        // Write your code here
        ArrayList<Integer> res = new ArrayList();
        for (int i = 1; i <= size; i++) {
            res.add(i);
        }
        return res;
    }
}

results matching ""

    No results matching ""