[Java] 13.7. 集合與陣列管理工具

Collections類別與Arrays類別,幫助您快速管理集合與陣列中的物件。

 

image/svg+xml13.7. 集合與陣列管理⼯具 - Collections & Arrays import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "K12345678" , "Jack" , 20 , " 男⽣ " , "048679" , 40000 ); Employee e2 = new Employee( "K00000000" , "Eric" , 25 , " 男⽣ " , "041121" , 45000 ); Employee e3 = new Employee( "K11111111" , "Mary" , 18 , " 女⽣ " , "050021" , 30000 ); Employee e4 = new Employee( "K22222222" , "Jack" , 19 , " 男⽣ " , "051212" , 42000 ); ArrayList l = new ArrayList (); l.add ( e1 ); l.add ( e2 ); l.add ( e3 ); l.add ( e4 ); for ( Object o : l){ Employee e = (Employee)o; System.out.print(e.name + " " ); System.out.println(e. salary ); } } } 執⾏結果 Jack 40000 Eric 45000 Mary 30000 Jack 42000 class Employee extends Person implements Comparable{ String employeeID; int salary; // 略過⼀些程式 public int compareTo ( Object o){ Employee p = (Employee)o; if ( this .salary > p.salary ) return 1 ; if ( this .salary < p.salary ) return - 1 ; return 0 ; } } import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "K12345678" , "Jack" , 20 , " 男⽣ " , "048679" , 40000 ); Employee e2 = new Employee( "K00000000" , "Eric" , 25 , " 男⽣ " , "041121" , 45000 ); Employee e3 = new Employee( "K11111111" , "Mary" , 18 , " 女⽣ " , "050021" , 30000 ); Employee e4 = new Employee( "K22222222" , "Jack" , 19 , " 男⽣ " , "051212" , 42000 ); ArrayList l = new ArrayList (); l.add ( e1 ); l.add ( e2 ); l.add ( e3 ); l.add ( e4 ); Collections.sort ( l ); for ( Object o : l){ Employee e = (Employee)o; System.out.print(e.name + " " ); System.out.println(e. salary ); } } } 執⾏結果 Mary 30000 Jack 40000 Jack 42000 Eric 45000 import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "K12345678" , "Jack" , 20 , " 男⽣ " , "048679" , 40000 ); Employee e2 = new Employee( "K00000000" , "Eric" , 25 , " 男⽣ " , "041121" , 45000 ); Employee e3 = new Employee( "K11111111" , "Mary" , 18 , " 女⽣ " , "050021" , 30000 ); Employee e4 = new Employee( "K22222222" , "Jack" , 19 , " 男⽣ " , "051212" , 42000 ); ArrayList l = new ArrayList (); l.add ( e1 ); l.add ( e2 ); l.add ( e3 ); l.add ( e4 ); Collections.sort ( l ); System.out.println( Collections.binarySearch ( l , e1 )); System.out.println(Collections.binarySearch( l , e2 )); System.out.println(Collections.binarySearch( l , e3 )); System.out.println(Collections.binarySearch( l , e4 )); } } 執⾏結果 1 3 0 2 import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "K12345678" , "Jack" , 20 , " 男⽣ " , "048679" , 40000 ); Employee e2 = new Employee( "K00000000" , "Eric" , 25 , " 男⽣ " , "041121" , 45000 ); Employee e3 = new Employee( "K11111111" , "Mary" , 18 , " 女⽣ " , "050021" , 30000 ); Employee e4 = new Employee( "K22222222" , "Jack" , 19 , " 男⽣ " , "051212" , 42000 ); ArrayList l = new ArrayList (); l.add ( e1 ); l.add ( e2 ); l.add ( e3 ); l.add ( e4 ); Collections.sort ( l ); System.out.println( Collections.binarySearch ( l , e1 )); System.out.println(Collections.binarySearch( l , e2 )); System.out.println(Collections.binarySearch( l , e3 )); System.out.println(Collections.binarySearch( l , e4 )); } } 執⾏結果 1 3 0 2 import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "K12345678" , "Jack" , 20 , " 男⽣ " , "048679" , 40000 ); Employee e2 = new Employee( "K00000000" , "Eric" , 25 , " 男⽣ " , "041121" , 45000 ); Employee e3 = new Employee( "K11111111" , "Mary" , 18 , " 女⽣ " , "050021" , 30000 ); Employee e4 = new Employee( "K22222222" , "Jack" , 19 , " 男⽣ " , "051212" , 42000 ); Employee [] ary = new Employee [ 4 ]; ary [ 0 ] = e1 ; ary [ 1 ] = e2 ; ary [ 2 ] = e3 ; ary [ 3 ] = e4 ; for ( Object o : ary){ Employee e = (Employee)o; System.out.print(e.name + " " ); System.out.println(e. salary ); } } } 執⾏結果 Jack 40000 Eric 45000 Mary 30000 Jack 42000 import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "K12345678" , "Jack" , 20 , " 男⽣ " , "048679" , 40000 ); Employee e2 = new Employee( "K00000000" , "Eric" , 25 , " 男⽣ " , "041121" , 45000 ); Employee e3 = new Employee( "K11111111" , "Mary" , 18 , " 女⽣ " , "050021" , 30000 ); Employee e4 = new Employee( "K22222222" , "Jack" , 19 , " 男⽣ " , "051212" , 42000 ); Employee [] ary = new Employee [ 4 ]; ary [ 0 ] = e1 ; ary [ 1 ] = e2 ; ary [ 2 ] = e3 ; ary [ 3 ] = e4 ; Arrays.sort ( ary ); for ( Object o : ary){ Employee e = (Employee)o; System.out.print(e.name + " " ); System.out.println(e. salary ); } } } 執⾏結果 Mary 30000 Jack 40000 Jack 42000 Eric 45000 import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "K12345678" , "Jack" , 20 , " 男⽣ " , "048679" , 40000 ); Employee e2 = new Employee( "K00000000" , "Eric" , 25 , " 男⽣ " , "041121" , 45000 ); Employee e3 = new Employee( "K11111111" , "Mary" , 18 , " 女⽣ " , "050021" , 30000 ); Employee e4 = new Employee( "K22222222" , "Jack" , 19 , " 男⽣ " , "051212" , 42000 ); Employee [] ary = new Employee [ 4 ]; ary [ 0 ] = e1 ; ary [ 1 ] = e2 ; ary [ 2 ] = e3 ; ary [ 3 ] = e4 ; Arrays.sort ( ary ); System.out.println( Arrays.binarySearch ( ary , e1 )); System.out.println(Arrays.binarySearch(ary, e2)); System.out.println(Arrays.binarySearch(ary, e3)); System.out.println(Arrays.binarySearch(ary, e4)); } } 執⾏結果 1 3 0 2 import java.util.*; class Main{ public static void main( String [] args){ Employee e1 = new Employee( "K12345678" , "Jack" , 20 , " 男⽣ " , "048679" , 40000 ); Employee e2 = new Employee( "K00000000" , "Eric" , 25 , " 男⽣ " , "041121" , 45000 ); Employee e3 = new Employee( "K11111111" , "Mary" , 18 , " 女⽣ " , "050021" , 30000 ); Employee e4 = new Employee( "K22222222" , "Jack" , 19 , " 男⽣ " , "051212" , 42000 ); Employee [] ary = new Employee [ 4 ]; ary [ 0 ] = e1 ; ary [ 1 ] = e2 ; ary [ 2 ] = e3 ; ary [ 3 ] = e4 ; Arrays.sort ( ary ); System.out.println( Arrays.binarySearch ( ary , e1 )); System.out.println(Arrays.binarySearch(ary, e2)); System.out.println(Arrays.binarySearch(ary, e3)); System.out.println(Arrays.binarySearch(ary, e4)); } } 執⾏結果 0 1 -1 -2 J13_7_1 Main.java 1. 建立 4 個員⼯物件,其擁有不同的薪資。 J13_7_2 Main.java 3. 加入 4 個員⼯物件⾄ ArrayList 物件中。 4. 當然取得的資料是不會依照員⼯的薪資來排序的。 要如何才能讓員⼯物件在 List 中也能夠進⾏排序呢? 2. 在這裡建立的是⼀個實作 List 介⾯的類別的物件 ArrayList ,其並沒有提供排序的功能。