How to add elements to an array without using ArrayList?

I'm working on an assignment where I need to take create an array based on user inputs. The first item in the array is an original value (xmin), and subsequent numbers are the previous number plus another user inputted value (incretement). So if xmin is 1 and incretement is 2, the array would be { 1, 3, 5, 7 } etc, and the program fills the array up to length n (another user input). The code for this part works currently but I just realized we're not allowed to use StringBuilder, Arrays class (so no ArrayList), or any class in

The second part of the assignment is to create a second array of y values based on the array of x values. So I would have to read each value of the x array, apply a function to it, and put the new value in the y array. The code for this section isn't working.

ArrayList<Double> x = new ArrayList<Double>();
int i;
double value = xmin;
for (i = 0; i < n - 1; i++) {
    value += increment;
ArrayList<Double> y = new ArrayList<Double>();
for (i = 0; i < n - 1; i++) {
    double read = x[i];
    double yValue = 20 * Math.abs(Math.sin(x));


  • I suppose you read n before you start filling the array? If yes, you can simply do

    double[] x = new double[n];
    x[0] = xmin;
    for (int i = 1; i < n; i++)
      x[i] = x[i-1] + increment;
    double[] y = new double[n];
    for (int i = 0; i < n; i++)
      y[i] = 20 * Math.abs(Math.sin(x[i]));

    You don't even need to preinitialize the values of your arrays, because they will be filled with the default value of 0