Is there any built-un function in Java to reverse the array from one specified index to another index?

huangapple go评论62阅读模式
英文:

Is there any built-un function in Java to reverse the array from one specified index to another index?

问题

例如:
我的数组是[1,2,3,4],我想要将索引从1到2的部分反转。因此,我的答案应为[1,3,2,4]

我该如何实现这个?

英文:

For example:
My array is [1,2,3,4] and I want to reverse it from index 1 to 2. So my answer should be [1,3,2,4].

How can I achieve this?

答案1

得分: 1

这很简单:

public static void reverseRangeClose(int[] arr, int fromIndexInclusive, int toIndexInclusive) {
    for (int i = fromIndexInclusive, j = toIndexInclusive; i < j; i++, j--) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

输出:

int[] arr = { 1, 2, 3, 4 };
System.out.println(Arrays.toString(arr));   // [1, 2, 3, 4]
reverseRangeClose(arr, 0, 2);
System.out.println(Arrays.toString(arr));   // [3, 2, 1, 4]
英文:

This is very simple:

public static void reverseRangeClose(int[] arr, int fromIndexInclusive, int toIndexInclusive) {
    for (int i = fromIndexInclusive, j = toIndexInclusive; i &lt; j; i++, j--) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

Output:

int[] arr = { 1, 2, 3, 4 };
System.out.println(Arrays.toString(arr));   // [1, 2, 3, 4]
reverseRangeClose(arr, 0, 2);
System.out.println(Arrays.toString(arr));   // [3, 2, 1, 4]

答案2

得分: 0

如果您将数组指定为对象数组而不是基本数组,则可以按以下方式进行操作。

Integer[] a = {1,2,3,4,5,6,7,8,9,10};
Collections.reverse(Arrays.asList(a).subList(3,7));
System.out.println(Arrays.toString(a));

它能够工作是因为Arrays.asList使用您的数组作为列表的支持。子列表指定要反转的列表(从而也反转了数组)的部分。

打印结果

[1, 2, 3, 7, 6, 5, 4, 8, 9, 10]

对于基本数组,您可以按以下方式操作。这就是@Andreas所谈论的内容。唯一的区别在于结束索引是“排除”的,以符合大多数语言中指定范围的标准做法。并且当起始索引大于结束索引时,我还会抛出异常。

public static void reverse(int[] a, int begin, int end) {
	if (begin > end) {
		throw new IllegalArgumentException(
				String.format("begin(%d) > end(%d)", begin, end));
	}
	for (int i = begin, j = end-1; i < j; i++, j--) {
		int t1 = a[i];
		a[i] = a[j];
		a[j] = t1;
	}
}
英文:

If you specify your array as an Object array and not a primitive array you can do it like this.

Integer[] a = {1,2,3,4,5,6,7,8,9,10};
Collections.reverse(Arrays.asList(a).subList(3,7));
System.out.println(Arrays.toString(a));

It works because the Arrays.asList uses your array to back up the list. The sublist specifies which part of the list (and thus the array) to reverse.

Prints

[1, 2, 3, 7, 6, 5, 4, 8, 9, 10]

For a primitive array, you can do it like this. This is what @Andreas was talking about. The only difference is that the end is exclusive to conform to standard practices in most languages when specifying ranges. And I also throw an exception when the beginning index is greater than the ending index.

public static void reverse(int[] a, int begin, int end) {
	if (begin &gt; end) {
		throw new IllegalArgumentException(
				String.format(&quot;begin(%d) &gt; end(%d)&quot;, begin, end));
	}
	for (int i = begin, j = end-1; i &lt; j; i++, j--) {
		int t1 = a[i];
		a[i] = a[j];
		a[j] = t1;
	}
}


</details>



huangapple
  • 本文由 发表于 2020年9月20日 00:57:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63971287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定