Quick actions

cmd+k|ctrl+k

Navigation

Languages

Array Gen

Snippet info

Language

Java

Visibility

public

Author

codecannamw

Created

2021-10-22T14:16:09.392317Z

Updated

2021-10-22T14:16:09.392317Z

class Main {
    public static int[] generate(int length, int difference) {
        int[] arrToGen = {};
        try {
            int[] arr = new int[length];
            // Check params
            if (length < 0) {
                throw new NegativeArraySizeException("Length cannot be negative!");
            }
            
            for (int i = 0; i < arr.length; i++) {
                arr[i] += (difference * i);
            }
            
            arrToGen = arr;
        } catch(NegativeArraySizeException e) {
            e.printStackTrace();
        }
        return arrToGen;
    }
    
    public static void main(String[] args) {
        int[] arr = generate(0, -3);
        
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr.length);
        }
    }
}
INFO