如何在Java中将Arrays值定为循环格式? [英] how to order Arrays value as circular format in java?
本文介绍了如何在Java中将Arrays值定为循环格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数组值就像
String []值= {"1","2","3","4","5","6","7","8","9","10};
假设我将值"5"传递给tat数组,则应按
suppose if i pass value "5" to tat array, it should be ordered as like
{"5","6","7","8","9","10",1","2","3","4"};...
该怎么办?请有人帮忙吗?谢谢你
how to do?plz anyone help? thank u
推荐答案
您需要的是旋转.您可以使用 Collections.rotate()
方法.将数组转换为列表并将其传递给方法.由于列表由数组支持,因此将旋转数组到位:
What you need is called rotation. You can use Collections.rotate()
method. Convert the array to a list and pass it to the method. This will rotate the array in place since the list is backed by the array:
String[] value = {"1","2","3", "4","5","6","7","8","9","10"};
Collections.rotate(Arrays.asList(value), 5);
上面的代码会将数组旋转5距离.得到的 value
数组:
The above code will rotate the array by a distance of 5. The resulting value
array:
[6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
这篇关于如何在Java中将Arrays值定为循环格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文