Java 数组元素倒序的三种方式(小结)
作者:Ramer-F 时间:2022-04-01 22:59:19
将数组元素反转有多种实现方式,这里介绍常见的三种.
直接数组元素对换
@Test
public void testReverseSelf() throws Exception {
System.out.println("use ReverseSelf");
String[] strings = { "ramer", "jelly", "bean", "cake" };
System.out.println("\t" + Arrays.toString(strings));
for (int start = 0, end = strings.length - 1; start < end; start++, end--) {
String temp = strings[end];
strings[end] = strings[start];
strings[start] = temp;
}
System.out.println("\t" + Arrays.toString(strings));
}
使用ArrayList: ArrayList存入和取出的顺序是一样的,可以利用这里特性暂时存储数组元素.
@Test
public void testArrayList() throws Exception {
System.out.println("use ArrayList method");
String[] strings = { "ramer", "jelly", "bean", "cake" };
System.out.println("\t" + Arrays.toString(strings));
List<String> list = new ArrayList<>(strings.length);
for (int i = strings.length - 1; i >= 0; i--) {
list.add(strings[i]);
}
strings = list.toArray(strings);
System.out.println("\t" + Arrays.toString(strings));
}
使用Collections和Arrays工具类
@Test
public void testCollectionsReverse() throws Exception {
System.out.println("use Collections.reverse() method");
String[] strings = { "ramer", "jelly", "bean", "cake" };
System.out.println("\t" + Arrays.toString(strings));
// 这种方式仅针对引用类型,对于基本类型如:
// char[] cs = {'a','b','c','g','d'};
// 应该定义或转换成对应的引用类型:
// Character[] cs = {'a','b','c','g','d'};
Collections.reverse(Arrays.asList(strings));
System.out.println("\t" + Arrays.toString(strings));
}
速度测试:
@Test
public void testTimeDuration() throws Exception {
recordTime(ArrayReverse.class,"testCollectionsReverse");
recordTime(ArrayReverse.class,"testArrayList");
recordTime(ArrayReverse.class,"testReverseSelf");
}
private static String[] strings = new String[1000000];
{
for (int i = 0; i < 1000000; i++) {
strings[i] = String.valueOf(i);
}
}
/**
* 记录操作执行总时间.
*
* @param <T> the generic type
* @param clazz the clazz
* @param methodName the method name
*/
public <T> void recordTime(Class<T> clazz, String methodName) {
long start = System.currentTimeMillis();
System.out.println("start: " + start);
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method method : declaredMethods) {
String name = method.getName();
if (name.equals(methodName)) {
try {
method.invoke(clazz.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
System.out.println("end: " + end);
System.out.println("duration: " + (end - start) + " ms");
}
测试结果:
使用Collections和Arrays工具类: 12 ms
使用ArrayList: 7 ms
直接数组元素对换: 4 ms
当数据量越来越大时,使用ArrayList的方式会变得很慢.
直接使用数组元素对换,总是最快完成.
总结: 使用Collections和Arrays工具类反转数组元素更简单,但是在原数组上操作时速度更快,并且占用最少的内存.
来源:https://blog.csdn.net/u011699931/article/details/52554787
标签:Java,数组,元素,倒序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解C#如何实现屏幕放大和取色功能
2022-04-15 04:00:37
![](https://img.aspxhome.com/file/2023/8/106438_0s.png)
java控制台输入示例分享
2023-11-28 02:28:05
C语言字符串大小比较
2022-06-02 14:15:17
Spring Mvc下实现以文件流方式下载文件的方法示例
2023-11-12 10:14:22
Unity实现简单手势识别
2022-10-14 07:52:05
java实习--每天打卡十道面试题!
2021-12-06 15:22:19
![](https://img.aspxhome.com/file/2023/2/85522_0s.png)
RecyclerView实现侧滑拖拽功能
2023-03-10 16:09:50
java分页工具类的使用方法
2023-08-17 02:00:14
Kotlin实现在类里面创建main函数
2022-08-26 20:11:13
![](https://img.aspxhome.com/file/2023/7/94997_0s.jpg)
Java(TM) Platform SE binary 打开jar文件的操作
2021-10-02 00:08:12
![](https://img.aspxhome.com/file/2023/4/62534_0s.jpg)
Android中使用CircleImageView和Cardview制作圆形头像的方法
2022-04-19 05:41:35
![](https://img.aspxhome.com/file/2023/2/92582_0s.gif)
SpringBoot资源文件的存放位置设置方式
2023-02-26 10:37:00
![](https://img.aspxhome.com/file/2023/0/84240_0s.png)
C#程序打成 一键安装包-InstallShield教程
2022-11-19 06:02:07
![](https://img.aspxhome.com/file/2023/6/88406_0s.jpg)
Android调用应用安装界面方法
2021-05-25 18:29:20
![](https://img.aspxhome.com/file/2023/4/94954_0s.jpg)
Android自定义单选多选下拉列表的实例代码
2022-06-08 15:53:33
![](https://img.aspxhome.com/file/2023/9/102589_0s.gif)
代码详解java里的“==”和“equels”区别
2023-01-20 04:08:10
Maven将代码及依赖打成一个Jar包的方式详解(最新推荐)
2022-03-31 06:52:47
![](https://img.aspxhome.com/file/2023/3/103843_0s.png)
Android 实现永久保存数据的方法详解
2023-06-23 22:13:23
Java Idea高效率配置技巧实例解析
2023-06-05 13:24:42
![](https://img.aspxhome.com/file/2023/7/61407_0s.jpg)
java利用递归算法实现对文件夹的删除功能
2023-07-26 06:37:02