JAVA中对象数组和基本类型数组的互换以及数组和MAP的转换:
以下例子是:
1.0克隆和反转对象数组;
2.0在数组中搜索特定项;
3.0由多维数组生成;
呵呵 这些可能在项目经常用到.我只是在我项目中碰到的问题做了一下电子笔记而已.
package JakartaCommonsCookbook;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
public class UseArrayUtils {
public static void main(String[] args) {
cloneArray();
ArrayUtilsContains();
weightArrayConverToMap();
}
//001克隆和反转对象数组
public static void cloneArray(){
int[] array = {1, 3, 2, 3, 5, 6};
int[] reversed = ArrayUtils.clone(array);
ArrayUtils.reverse(reversed);
System.out.println("----------cloneArray- and reverse---------- ");
System.out.println("Original is : "+ArrayUtils.toString(array));
System.out.println("reversed is : "+ArrayUtils.toString(reversed));
//primitiveArray to objectArray
long[] primitiveArray = new long[]{12,100,2992,3233};
Long[] objectArray = ArrayUtils.toObject(primitiveArray);
System.out.println("objectArray is : "+ArrayUtils.toString(objectArray));
}
public static void ArrayUtilsContains(){
String[] stringArray = {"red","green","blue","brown", "yellow", "red"};
boolean contiansBlue = ArrayUtils.contains(stringArray, "blue");
int indexOfRed = ArrayUtils.indexOf(stringArray, "red");
int lastIndexOfRed = ArrayUtils.lastIndexOf(stringArray, "red");
System.out.println("----------ArrayUtilsContains----------- ");
System.out.println("Array contains 'blue' is :? "+contiansBlue);
System.out.println("Index of red is : "+indexOfRed);
System.out.println("Last Index of red is : "+lastIndexOfRed);
}
public static void weightArrayConverToMap(){
Object[] weightArray = new Object[][]{
{"H",new Double(1.007)},
{"He",new Double(4.007)},
{"Li",new Double(6.007)},
{"B",new Double(5.007)},
{"Be",new Double(2.007)},
{"C",new Double(11.007)},
{"N",new Double(12.007)},
{"O",new Double(13.007)}
};
// create Map mapping colors
Map map = ArrayUtils.toMap(weightArray);
Double hydorgenWeight = (Double) map.get("H");
System.out.println("----------weightArrayConverToMap-----------");
System.out.println(hydorgenWeight);
System.out.println((Double) map.get("He"));
System.out.println((Double) map.get("O"));
}
}
执行结果为:
----------cloneArray- and reverse----------
Original is : {1,3,2,3,5,6}
reversed is : {6,5,3,2,3,1}
objectArray is : {12,100,2992,3233}
----------ArrayUtilsContains-----------
Array contains 'blue' is :? true
Index of red is : 0
Last Index of red is : 5
----------weightArrayConverToMap-----------
1.007
4.007
13.007
以下例子是:
1.0克隆和反转对象数组;
2.0在数组中搜索特定项;
3.0由多维数组生成;
呵呵 这些可能在项目经常用到.我只是在我项目中碰到的问题做了一下电子笔记而已.
package JakartaCommonsCookbook;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
public class UseArrayUtils {
public static void main(String[] args) {
cloneArray();
ArrayUtilsContains();
weightArrayConverToMap();
}
//001克隆和反转对象数组
public static void cloneArray(){
int[] array = {1, 3, 2, 3, 5, 6};
int[] reversed = ArrayUtils.clone(array);
ArrayUtils.reverse(reversed);
System.out.println("----------cloneArray- and reverse---------- ");
System.out.println("Original is : "+ArrayUtils.toString(array));
System.out.println("reversed is : "+ArrayUtils.toString(reversed));
//primitiveArray to objectArray
long[] primitiveArray = new long[]{12,100,2992,3233};
Long[] objectArray = ArrayUtils.toObject(primitiveArray);
System.out.println("objectArray is : "+ArrayUtils.toString(objectArray));
}
public static void ArrayUtilsContains(){
String[] stringArray = {"red","green","blue","brown", "yellow", "red"};
boolean contiansBlue = ArrayUtils.contains(stringArray, "blue");
int indexOfRed = ArrayUtils.indexOf(stringArray, "red");
int lastIndexOfRed = ArrayUtils.lastIndexOf(stringArray, "red");
System.out.println("----------ArrayUtilsContains----------- ");
System.out.println("Array contains 'blue' is :? "+contiansBlue);
System.out.println("Index of red is : "+indexOfRed);
System.out.println("Last Index of red is : "+lastIndexOfRed);
}
public static void weightArrayConverToMap(){
Object[] weightArray = new Object[][]{
{"H",new Double(1.007)},
{"He",new Double(4.007)},
{"Li",new Double(6.007)},
{"B",new Double(5.007)},
{"Be",new Double(2.007)},
{"C",new Double(11.007)},
{"N",new Double(12.007)},
{"O",new Double(13.007)}
};
// create Map mapping colors
Map map = ArrayUtils.toMap(weightArray);
Double hydorgenWeight = (Double) map.get("H");
System.out.println("----------weightArrayConverToMap-----------");
System.out.println(hydorgenWeight);
System.out.println((Double) map.get("He"));
System.out.println((Double) map.get("O"));
}
}
执行结果为:
----------cloneArray- and reverse----------
Original is : {1,3,2,3,5,6}
reversed is : {6,5,3,2,3,1}
objectArray is : {12,100,2992,3233}
----------ArrayUtilsContains-----------
Array contains 'blue' is :? true
Index of red is : 0
Last Index of red is : 5
----------weightArrayConverToMap-----------
1.007
4.007
13.007