public static void main(String[] args) { List<String> list = Arrays.asList( "浙江移動", "江蘇移動", "浙江電信", "上海移動", "北京移動", "浙江聯通"); Collections.sort(list); System.out.println(StringUtils.join(list.toArray(), ",")); }
運行結果:
上海移動,北京移動,江蘇移動,浙江電信,浙江移動,浙江聯通
public static void main(String[] args) { List<String> list = Arrays.asList( "浙江移動", "江蘇移動", "浙江電信", "上海移動", "北京移動", "浙江聯通"); Collections.sort(list, Collator.getInstance(java.util.Locale.CHINA)); System.out.println(StringUtils.join(list.toArray(), ",")); }
運行結果:
北京移動,江蘇移動,上海移動,浙江電信,浙江聯通,浙江移動
public static void main(String[] args) { List<String> list = Arrays.asList( "浙江移動", "江蘇移動", "浙江電信", "上海移動", "北京移動", "浙江聯通"); Collections.sort(list, Collator.getInstance(java.util.Locale.CHINA)); Collections.reverse(list); //反轉整個數組 System.out.println(StringUtils.join(list.toArray(), ",")); }
運行結果:
浙江移動,浙江聯通,浙江電信,上海移動,江蘇移動,北京移動
public static void main(String[] args) { List<String> list = Arrays.asList( "浙江移動", "江蘇移動", "浙江電信", "上海移動", "北京移動", "浙江聯通"); Collections.shuffle(list); //隨機排序 System.out.println(StringUtils.join(list.toArray(), ",")); Collections.shuffle(list); //隨機排序 System.out.println(StringUtils.join(list.toArray(), ",")); } 運行結果: 浙江電信,浙江移動,上海移動,江蘇移動,浙江聯通,北京移動 浙江移動,江蘇移動,北京移動,上海移動,浙江電信,浙江聯通
public static void main(String[] args) { List<String> list = Arrays.asList( "浙江移動", "江蘇移動", "浙江電信", "上海移動", "北京移動", "浙江聯通"); Collections.rotate(list, 1); //向右移動1 System.out.println(StringUtils.join(list.toArray(), ",")); Collections.rotate(list, -2); //向左移動2 System.out.println(StringUtils.join(list.toArray(), ",")); } 運行結果: 浙江聯通,浙江移動,江蘇移動,浙江電信,上海移動,北京移動 江蘇移動,浙江電信,上海移動,北京移動,浙江聯通,浙江移動
6.換位(swap)
public static void main(String[] args) { List<String> list = Arrays.asList( "浙江移動", "江蘇移動", "浙江電信", "上海移動", "北京移動", "浙江聯通"); Collections.swap(list, 0, 1); //向右移動1 System.out.println(StringUtils.join(list.toArray(), ",")); } 運行結果: 江蘇移動,浙江移動,浙江電信,上海移動,北京移動,浙江聯通