ChuckDivebomb
ChuckDivebomb

Reputation: 55

Create intermediate values in a Java array

I need to find a way to create a new array in Java that will insert intermediate values between the values of an already-existing array. I can create a new array of the required length, but I'm unsure as to how to proceed. Specifically, I need to write a method that takes as input an array of integers and returns an array of integers 3 times (minus 2) as long as the input array. The returned array should be the high definition array, with values 1/3 and 2/3 between the two values. For instance, given input array

{400,500,600}

it should return

{400,433,466,500,533,566}

for any length array. Any advice?

Upvotes: 1

Views: 751

Answers (4)

BlueMoon93
BlueMoon93

Reputation: 2976

public int[] getValues(int[] a){
    int [] b = new int[a.length*3-3];

    for(int i=0; i<a.length()-1; i++){
        b[i*3]=a[i];
        b[i*3+1]=a[i]+(a[i+1]-a[i])/3;
        b[i*3+2]=a[i]+(a[i+1]-a[i])/3*2;
    }
}

Upvotes: 1

helderdarocha
helderdarocha

Reputation: 23627

I made a recursive one, using only arrays (no collections), and left the intermediate System.out.println()s so you can see how it works.

public class App {

    public static void main(String[] args) {
        int[] array = {400, 500, 600, 800};
        int[] newArray = new int[array.length * 3 - 2];
        makeArray(array, newArray, 0);

        System.out.println("\nResult: " + Arrays.toString(newArray));
    }

    public static void makeArray(int[] array, int[] newArray, int offset) {

        System.out.println("Recursion: " + (offset/3+1));
        System.out.println("--> array: "+Arrays.toString(array));
        System.out.println("--> newArray: "+Arrays.toString(newArray));

        if (array.length == 1) {
            System.arraycopy(array, 0, newArray, newArray.length-1, array.length);
        } else {
            int[] arrayPart = {array[0], array[1]};
            int[] rest = new int[array.length - 1];

            System.arraycopy(array, 1, rest, 0, rest.length);

            int third = (int)((arrayPart[1] - arrayPart[0])/3);
            int[] appendArray = {arrayPart[0], arrayPart[0]+third, arrayPart[0]+third*2};

            System.arraycopy(appendArray, 0, newArray, offset, appendArray.length);
            makeArray(rest, newArray, offset + 3);
        }
    }
}

This is the output:

Recursion: 1
--> array: [400, 500, 600, 800]
--> newArray: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Recursion: 2
--> array: [500, 600, 800]
--> newArray: [400, 433, 466, 0, 0, 0, 0, 0, 0, 0]
Recursion: 3
--> array: [600, 800]
--> newArray: [400, 433, 466, 500, 533, 566, 0, 0, 0, 0]
Recursion: 4
--> array: [800]
--> newArray: [400, 433, 466, 500, 533, 566, 600, 666, 732, 0]

Result: [400, 433, 466, 500, 533, 566, 600, 666, 732, 800]

Upvotes: 0

Thresh
Thresh

Reputation: 470

This would be my solution to the given problem

public static int[] highdef(int[] input) {
    int[] result = new int[input.length * 3 - 2];

    for (int i = 0; i < input.length - 1; i++) {
        result[i * 3] = input[i];
        result[i * 3 + 1] = input[i] + (Math.abs(input[i + 1] - input[i]) / 3);
        result[i * 3 + 2] = input[i] + (Math.abs(input[i + 1] - input[i]) / 3) * 2;
    }
    result[result.length - 1] = input[input.length - 1];
    return result;
}

The idea is that you compute the difference between each element and the next and compute 1/3 and 2/3 the difference to add it to the original value. This is dynamic enough as it uses the size of the incoming input array.

Upvotes: 1

RMachnik
RMachnik

Reputation: 3694

List<Integer> list= new ArrayList<Integer>; list.add(400); list.add(500); list.add(600);

and there you can iterate oveer list and add values...

        list.add(1,433);
        list.add(2,466);
        list.add(4,533);
        list.add(5,566);

Upvotes: 0

Related Questions