Java 數(shù)組元素倒序的三種方式(小結(jié))
將數(shù)組元素反轉(zhuǎn)有多種實現(xiàn)方式,這里介紹常見的三種.
直接數(shù)組元素對換
@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存入和取出的順序是一樣的,可以利用這里特性暫時存儲數(shù)組元素.
@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'};
// 應該定義或轉(zhuǎn)換成對應的引用類型:
// 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);
}
}
/**
* 記錄操作執(zhí)行總時間.
*
* @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");
}
測試結(jié)果:
使用Collections和Arrays工具類: 12 ms
使用ArrayList: 7 ms
直接數(shù)組元素對換: 4 ms
當數(shù)據(jù)量越來越大時,使用ArrayList的方式會變得很慢.
直接使用數(shù)組元素對換,總是最快完成.
總結(jié): 使用Collections和Arrays工具類反轉(zhuǎn)數(shù)組元素更簡單,但是在原數(shù)組上操作時速度更快,并且占用最少的內(nèi)存.
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MybatisPlus插件自動維護更新和創(chuàng)建時間方式
這篇文章主要介紹了MybatisPlus插件自動維護更新和創(chuàng)建時間方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
springboot中如何將logback切換為log4j2
springboot默認使用logback作為日志記錄框架,常見的日志記錄框架有l(wèi)og4j、logback、log4j2,這篇文章我們來學習怎樣將logbak替換為log4j2,需要的朋友可以參考下2023-06-06
Java8加java10等于Java18的版本查看及特性詳解
這篇文章主要為大家介紹了Java?8加java10等于Java18的各個版本要點詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Spring Data MongoDB中實現(xiàn)自定義級聯(lián)的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Data MongoDB中實現(xiàn)自定義級聯(lián)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11

