Java程序拆分数组并将第一部分添加到末尾
有一个给定的数组并从指定位置拆分它,并将数组的第一部分添加到末尾。
例子:
Input : arr[] = {12, 10, 5, 6, 52, 36}
k = 2
Output : arr[] = {5, 6, 52, 36, 12, 10}
Explanation : Split from index 2 and first
part {12, 10} add to the end .
Input : arr[] = {3, 1, 2}
k = 1
Output : arr[] = {1, 2, 3}
Explanation : Split from index 1 and first
part add to the end.
// Java program to split array and move first
// part to end.
import java.util.*;
import java.lang.*;
class GFG {
public static void splitArr(int arr[], int n, int k)
{
for (int i = 0; i < k; i++) {
// Rotate array by 1.
int x = arr[0];
for (int j = 0; j < n - 1; ++j)
arr[j] = arr[j + 1];
arr[n - 1] = x;
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 12, 10, 5, 6, 52, 36 };
int n = arr.length;
int position = 2;
splitArr(arr, 6, position);
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
}
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>
输出:
5 6 52 36 12 10
请参阅有关拆分数组的完整文章并将第一部分添加到末尾以获取更多详细信息!