Contents

About implementing counting up (with.Java)

   Sep 5, 2023     1 min read

Implementing a count up (with.Java).

Weโ€™re going to learn by solving a coding test problem, reflecting on the problem we solved, and exploring other ways to solve it. Letโ€™s start with the problem.

Problem

Given the integers start_num and end_num, complete the solution function so that it returns a list containing the numbers from start_num to end_num in order.Example input/output

Example input and output

start_num: 3 end_num: 10 result: [3, 4, 5, 6, 7, 8, 9, 10]

My solution to the problem

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<Integer> solution(int start_num, int end_num) {
        List<Integer> result = new ArrayList<>();

        for (int i = start_num; i <= end_num; i++) {
            result.add(i);
        }

        } return result;
    }
}
Solution

Iterates over the numbers from the starting number start_num to the ending number end_num, adding them to the result list set to the initial value of the ArrayList, and finally returns the list.