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)文章
Java 在PPT中創(chuàng)建散點圖的實現(xiàn)示例
本文將以Java代碼示例展示如何在PPT幻燈片中創(chuàng)建散點圖表。文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
java通過Callable和Future來接收線程池的執(zhí)行結(jié)果
這篇文章主要介紹了java通過Callable和Future來接收線程池的執(zhí)行結(jié)果,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
HttpClient的RedirectStrategy重定向處理核心機制
這篇文章主要為大家介紹了HttpClient的RedirectStrategy重定向處理核心機制源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

