java数组元素倒序的三种方法

将数组元素反转有多种实现方式,这里介绍常见的三种.

  • 直接数组元素对换
@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));
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 使用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));
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 使用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));
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 速度测试:
@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");
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 测试结果:

    使用Collections和Arrays工具类: 12 ms
    使用ArrayList: 7 ms
    直接数组元素对换: 4 ms
    当数据量越来越大时,使用ArrayList的方式会变得很慢.
    直接使用数组元素对换,总是最快完成.

  • 总结: 使用Collections和Arrays工具类反转数组元素更简单,但是在原数组上操作时速度更快,并且占用最少的内存.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值