如何在 Java 中反转 int 数组?
我正在尝试反转 Java 中的 int 数组。
此方法不会反转数组。
for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
这有什么问题吗?
I am trying to reverse an int array in Java.
This method does not reverse the array.
for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
What is wrong with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
时间复杂度为 o(n) ,空间复杂度为 o(1) 的解决方案。
Solution with o(n) time complexity and o(1) space complexity.
这样做不是更不容易出错吗?
Wouldn't doing it this way be much more unlikely for mistakes?
使用 XOR 解决方案来避免临时变量,您的代码应如下所示
请参阅此链接以获得更好的解释:
http://betterexplained.com/articles/swap-two-variables-using-xor/
Using the XOR solution to avoid the temp variable your code should look like
See this link for a better explanation:
http://betterexplained.com/articles/swap-two-variables-using-xor/
反转 Array 的 2 种方法。
使用For循环交换元素直到中点,时间复杂度为O(n/2)。
}
使用内置函数 (Collections.reverse())
}
输出:[6, 5, 4, 3, 2, 1]
2 ways to reverse an Array .
Using For loop and swap the elements till the mid point with time complexity of O(n/2).
}
Using built in function (Collections.reverse())
}
Output : [6, 5, 4, 3, 2, 1]
对非基本类型数组使用泛型的实现。
A implementation using generics for arrays of non primitive types.
下面是在您的机器上运行的完整程序。
对于使用数组的矩阵程序 这将是一个很好的来源< /a>.浏览链接。
below is the complete program to run in your machine.
For programs on matrix using arrays this will be the good source.Go through the link.
这是一个简单的实现,用于反转任何类型的数组,以及完全/部分支持。
这是相应的单元测试
Here is a simple implementation, to reverse array of any type, plus full/partial support.
Here is the corresponding Unit Test
这是我想出的:
Here is what I've come up with:
要反转 int 数组,请向上交换项目,直到到达中点,如下所示:
按照您执行此操作的方式,将每个元素交换两次,因此结果与初始列表相同。
To reverse an int array, you swap items up until you reach the midpoint, like this:
The way you are doing it, you swap each element twice, so the result is the same as the initial list.
使用 Commons.Lang,您可以简单地使用
大多数时候,它更快、更安全当它们解决您的问题时,坚持使用已经过单元测试和用户测试的易于使用的库。
With Commons.Lang, you could simply use
Most of the time, it's quicker and more bug-safe to stick with easily available libraries already unit-tested and user-tested when they take care of your problem.
java.util.Collections.reverse()
可以反转java.util.List
且java.util.Arrays.asList()
返回一个list 包装了您传递给它的特定数组,因此yourArray
在调用Collections.reverse()
后会反转。成本只是创建一个列表对象,不需要额外的库。
塔里克及其评论者的答案中提出了类似的解决方案,但我认为这个答案会更简洁,更容易解析。
java.util.Collections.reverse()
can reversejava.util.List
s andjava.util.Arrays.asList()
returns a list that wraps the the specific array you pass to it, thereforeyourArray
is reversed after the invocation ofCollections.reverse()
.The cost is just the creation of one List-object and no additional libraries are required.
A similar solution has been presented in the answer of Tarik and their commentors, but I think this answer would be more concise and more easily parsable.
我认为,如果您声明显式变量来跟踪在循环的每次迭代中交换的索引,那么遵循算法的逻辑会更容易一些。
我还认为在 while 循环中执行此操作更具可读性。
I think it's a little bit easier to follow the logic of the algorithm if you declare explicit variables to keep track of the indices that you're swapping at each iteration of the loop.
I also think it's more readable to do this in a while loop.
使用流来反转
这里已经有很多答案,主要集中在就地修改数组。但为了完整起见,这里有另一种方法,使用 Java 流来保留原始数组并创建一个新的反转数组:
Use a stream to reverse
There are already a lot of answers here, mostly focused on modifying the array in-place. But for the sake of completeness, here is another approach using Java streams to preserve the original array and create a new reversed array:
对于Java 8,我们还可以使用
IntStream
来反转整数数组,如下所示:In case of Java 8 we can also use
IntStream
to reverse the array of integers as:Guava
使用 Google Guava 库:
Guava
Using the Google Guava library:
简单的for循环!
Simple for loop!
如果使用更原始的数据(即字符、字节、整数等),那么您可以执行一些有趣的异或运算。
If working with data that is more primitive (i.e. char, byte, int, etc) then you can do some fun XOR operations.
这会帮助你
This will help you
这就是我个人解决这个问题的方法。创建参数化方法背后的原因是允许对任何数组进行排序......而不仅仅是整数。
我希望你能从中有所收获。
This is how I would personally solve it. The reason behind creating the parametrized method is to allow any array to be sorted... not just your integers.
I hope you glean something from it.
有两种方法可以解决该问题:
1.在空间中反转数组。
步骤 1. 交换起始索引和结束索引处的元素。
步骤 2. 增加起始索引并减少结束索引。
步骤3. 迭代步骤1和步骤2,直到起始索引<1。 end index
为此,时间复杂度为 O(n),空间复杂度为 O(1)
在空间中反转数组的示例代码如下:
2.使用辅助数组反转数组。
步骤 1. 创建一个大小等于给定数组的新数组。
步骤 2. 从起始索引开始向新数组中插入元素
给定数组从结束索引开始。
为此,时间复杂度为 O(n),空间复杂度为 O(n)
使用辅助数组反转数组的示例代码如下:
此外,我们可以使用 Java 中的 Collections API 来执行此操作.
Collections API 在内部使用相同的反向空间方法。
使用 Collections API 的示例代码如下:
There are two ways to have a solution for the problem:
1. Reverse an array in space.
Step 1. Swap the elements at the start and the end index.
Step 2. Increment the start index decrement the end index.
Step 3. Iterate Step 1 and Step 2 till start index < end index
For this, the time complexity will be O(n) and the space complexity will be O(1)
Sample code for reversing an array in space is like:
2. Reverse an array using an auxiliary array.
Step 1. Create a new array of size equal to the given array.
Step 2. Insert elements to the new array starting from the start index, from the
given array starting from end index.
For this, the time complexity will be O(n) and the space complexity will be O(n)
Sample code for reversing an array with auxiliary array is like:
Also, we can use the Collections API from Java to do this.
The Collections API internally uses the same reverse in space approach.
Sample code for using the Collections API is like:
您的程序仅适用于
length = 0, 1
。您可以尝试:
Your program will work for only
length = 0, 1
.You can try :
上面有一些很好的答案,但这就是我的做法:
There are some great answers above, but this is how I did it:
简单地向后迭代数组是最有效的。
我不确定 Aaron 的解决方案是否会执行此 vi 此调用
Collections.reverse(list);
有谁知道吗?It is most efficient to simply iterate the array backwards.
I'm not sure if Aaron's solution does this vi this call
Collections.reverse(list);
Does anyone know?