This is not necessary in a normal program, but when creating a library, such processing may be necessary. The execution environment is JDK 1.6.
Tips0022.java
package jp.avaj.lib.algo;
import jp.avaj.lib.test.ArTest;
import jp.avaj.lib.test.L;
class Tips0022 {
public static void main(String[] args) {
//
String nonArray = "aaaa";
Integer[] integerArray = new Integer[]{
0,1,2,3
};
int[] intArray = new int[] {
0,1,2,3
};
String[] strArray = new String[] {
//Size zero
};
//
//I forgot what the above variables are, so let's find out
//
//Start a test case
ArTest.startTestCase("Tips0022");
//
boolean result;
//
// nonArray
{
//Check if it is an array
result = nonArray.getClass().isArray();
ArTest.isFalse("nonArray","result",result);
}
// integerArray
{
//Check if it is an array.
result = integerArray.getClass().isArray();
ArTest.isTrue("integerArray","result",result);
//Get the type of elements in an array
L.p("Elements of integerArray="+integerArray.getClass().getComponentType().getName());
//Check if the elements of the array are primitive
result = integerArray.getClass().getComponentType().isPrimitive();
ArTest.isFalse("integerArray","result",result);
}
// intArray
{
//Check if it is an array
result = intArray.getClass().isArray();
ArTest.isTrue("intArray","result",result);
//Get the type of elements in an array
L.p("Elements of intArray="+intArray.getClass().getComponentType().getName());
//Check if the elements of the array are primitive
result = intArray.getClass().getComponentType().isPrimitive();
ArTest.isTrue("intArray","result",result);
}
// strArray
{
//Check if it is an array
result = strArray.getClass().isArray();
ArTest.isTrue("strArray","result",result);
//Get the type of elements in an array
L.p("Elements of strArray="+strArray.getClass().getComponentType().getName());
//Check if the elements of the array are primitive
result = strArray.getClass().getComponentType().isPrimitive();
ArTest.isFalse("strArray","result",result);
}
//End the test case
ArTest.endTestCase();
}
}
The result is as follows.
Tips0022-result.txt
**** Tips0022 start ****
OK nonArray:result=false
OK integerArray:result=true
Elements of integerArray=java.lang.Integer
OK integerArray:result=false
OK intArray:result=true
Elements of intArray=int
OK intArray:result=true
OK strArray:result=true
Elements of strArray=java.lang.String
OK strArray:result=false
**** Tips0022 summary ****
test count = 7
success = 7