简体   繁体   中英

java : how to Get new array elements in start of loop as result array at end of loop

i am coding in java and i am a newbie. i have an array at start, then i go into loop, first copy the array to newarray, then delete the last element and then i rotate newarray by one index. means element at index 0 will be at index 1, and so on, and element at last index will be at index 0. now i am storing this rotated array in new result array. Now i have to use this result array at start of loop and continue the loop till i am left with only one element. this is where i am facing the issue.

here is the code of it

int[] arr= {1,2,3,4,5}; // this is my orignal array

    int[] newArr = new int[arr.length];
    int[] result = new int[newArr.length];
    for(int i = 1;i<n;i++)
    {

        newArr = Arrays.copyOf(arr, arr.length - i);
        System.out.println(Arrays.toString(newArr));// here i am deleting one array at last,

// code works fine upto here. but i want the next step as below commented code to perform in loop. which i am really unable to do.

        //System.arraycopy(newArr, 0, result, 1, newArr.length - 1);
        //result[0] = newArr[newArr.length - 1];
        //result = Arrays.copyOf(result,result.length-1);
        //System.out.println(Arrays.toString(result));
    }

output should come like this

[1, 2, 3, 4, 5] // original array

[1, 2, 3, 4] // array after deletion

[4, 1, 2, 3] // array after rotaion

[4, 1, 2]// again after deletion from previous array

[2, 4, 1]// array after rotaion

[2, 4]// again after deletion from previous array

[4, 2]// array after rotation

[4]

final answer is 4

any help would be greatful. thanks. please let me know if i need to be more specific about my issue.

I would not recommend doing everything in a single method. Start by writing a method to perform the rotation. I would name it rotate<\/code> and it might look something like

private static void rotate(int[] arr) {
    int v = arr[arr.length - 1];
    for (int i = arr.length - 1; i > 0; i--) {
        arr[i] = arr[i - 1];
    }
    arr[0] = v;
}

Here is one approach.

int[] arr = {1,2,3,4,5};
while (arr.length > 1) {
    System.out.println(Arrays.toString(arr));
    arr = deleteAndRotate(arr);
}
System.out.println(Arrays.toString(arr));

Your code will perform much better if you get rid of these intermediate arrays . If you have a few million elements in the array, your algorithm will require to allocate in memory a few million arrays which doesn't sound very performance-wise. From the clean-coding point of view, only one defensive copy is needed to preserve the source array intact. And the only one defensive copy is needed to preserve the source array intact. And the required operations will be done in place`.

The core of this algorithm is the variable len defined inside the method rotateAndDelete() , which denotes the boundary of the "current array" .

    public static void main(String[] args) {
        System.out.println(rotateAndDelete(new int[]{1, 2, 3, 4, 5}));
    }
    public static int rotateAndDelete(int[] source) {
        int len = source.length;
        int[] copy = Arrays.copyOf(source, len);
        while (len > 1) {
            len--; // deletion in-place - the last valid index becomes less by one
            rotate(copy, len);
        }
        return copy[0];
    }
    private static void rotate(int[] arr, int len) {
        int first = arr[len - 1];
        // shift all elements to the right
        for (int i = len - 1; i >= 1; i--) {
            arr[i] = arr[i - 1];
        }
        arr[0] = first;
    }

output

4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM