primitive 數(shù)組克隆及反轉(zhuǎn)排序
long[] array = { 1, 3, 2, 3, 5, 6 };
long[] reversed = ArrayUtils.clone( array );
ArrayUtils.reverse( reversed );
System.out.println( "Original: " + ArrayUtils.toString( array ) ); //打印
System.out.println( "Reversed: " + ArrayUtils.toString( reversed ) );
對(duì)象數(shù)組克隆及反轉(zhuǎn)排序
Long[] array = new Long[] { new Long(3), new Long(56), new Long(233) };
Long[] reversed = ArrayUtils.clone( array );
ArrayUtils.reverse( reversed );
primitive 數(shù)組與對(duì)象數(shù)組之間的轉(zhuǎn)換
long[] primitiveArray = new long[] { 12, 100, 2929, 3323 };
Long[] objectArray = ArrayUtils.toObject( primitiveArray );
Double[] doubleObjects = new Double[] { new Double( 3.22, 5.222, 3.221 ) };
double[] doublePrimitives = ArrayUtils.toPrimitive( doubleObject );
注意:對(duì)象數(shù)組可以含有null元素,primitive 數(shù)組則不容許含有null元素,所以對(duì)象數(shù)組轉(zhuǎn)換為primitive 數(shù)組時(shí),可以添入第二個(gè)參數(shù),當(dāng)碰到為null的元素時(shí)用其代替(如下,Double.NaN)。如果不添入第二個(gè)參數(shù),當(dāng)碰到為null的元素時(shí),則會(huì)拋出NullPointerException 。
double[] result = ArrayUtils.toPrimitive( resultObjArray, Double.NaN );
查找一個(gè)數(shù)組中是否含有特定的元素(查找對(duì)象數(shù)組時(shí),比較的是對(duì)象的equals()方法),及特定元素的第一次出現(xiàn)位置和最后一次出現(xiàn)位置
String[] stringArray = { "Red", "Orange", "Blue", "Brown", "Red" };
boolean containsBlue = ArrayUtils.contains( stringArray, "Blue" );
int indexOfRed = ArrayUtils.indexOf( stringArray, "Red");
int lastIndexOfRed = ArrayUtils.lastIndexOf( string, "Red" );
由二維對(duì)象數(shù)組創(chuàng)建一個(gè) Map
Object[] weightArray =
new Object[][] { {"H" , new Double( 1.007)},
{"He", new Double( 4.002)},
{"Li", new Double( 6.941)},
{"Be", new Double( 9.012)},
{"B", new Double(10.811)},
{"C", new Double(12.010)},
{"N", new Double(14.007)},
{"O", new Double(15.999)},
{"F", new Double(18.998)},
{"Ne", new Double(20.180)} };
Map weights = ArrayUtils.toMap( weightArray );
Double hydrogenWeight = (Double)weights.get( "H" );
注意:當(dāng)二維對(duì)象數(shù)組"key"值重復(fù)時(shí),創(chuàng)建的Map,后面的鍵-值對(duì)會(huì)把前面的覆蓋掉
http://m.tkk7.com/ronghao 榮浩原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:)
posted on 2005-12-13 18:48
ronghao 閱讀(1372)
評(píng)論(4) 編輯 收藏 所屬分類:
j2se基礎(chǔ)