[JAVA] Get the type of an array element to determine if it is an array

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

Recommended Posts

Get the type of an array element to determine if it is an array
If the parameter is an array, how to include it in Stopara's params.permit
What to do if you can't get the text of an element in Selenium
[Swift5] How to get an array and the complement of arrays
[Swift] How to get the number of elements in an array (super basic)
[Java] How to convert one element of a String type array to an Int type
Program to determine if it is a leap year
Library to check if it is an IPv4 format string
Let's understand the Array (Element) type!
How to output the value when there is an array in the array
The story of toString () starting with passing an array to System.out.println
How to get the length of an audio file in java
Compare the elements of an array (Java)
How to determine the number of parallels
Convert an array of strings to numbers
How to convert an array of Strings to an array of objects with the Stream API
If it is Ruby, it is efficient to make it a method and stock the processing.
Corresponds to a property whose type is an array when empty using JsonDeserializer
[Java] Program example to get the maximum and minimum values from an array
[Swift5] How to compare specific times to determine if it is before or after
What to do if you get an "A server is already running." Error when you try to start the rails server
How to get today's day of the week
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
[Java] How to get the authority of the folder
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
[Swift] If the support of the application is iOS 11 or later, it was not necessary to use Int and Int64 properly
Since the argument of link_to is nil (null) and an unexpected link was generated, I tried to verify it
Get the value from the array and find out what number it is included in
It's easy to get rid of it, but I've summarized the times method again.
How to request by passing an array to the query with HTTP Client of Ruby
What to do if you get the error Couldn't find Item without an ID
Create an app that uses the weather API to determine if you need an umbrella.
The end of catastrophic programming # 03 "Comparison of integers, if" a> b ", assume that it is" a --b> 0 ""
[Java] How to get the URL of the transition source
When the hover of Eclipse is hard to see
If you get tired of drawing an ER diagram
Convert the array of errors.full_messages to characters and output
[Ruby] Method to easily get the receiver type .class
The question of which is better, if or switch
Is it faster if the docker bind mount is readonly?
[Java] How to get the maximum value of HashMap
Is there an instance even if the constructor fails?
Mandels to create an image of the Mandelbrot set
[Android] How to get the setting language of the terminal
Ruby, Nokogiri: Get the element name of the selected node
[Rails] How to get the contents of strong parameters
[Swift] How to get the document ID of Firebase
Null value is entered when assigning to an array
It is difficult to use the empty string or date of DBUnit, so fix it and use it.
[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
[Memo] MyBatis's mysterious movement, set null to the element of the list object and return it.
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
How to specify an array in the return value / argument of the method in the CORBA IDL file
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
[Rails] What to do if you can't get an error message with the errors method
Even if I write the setting of STRICT_QUOTE_ESCAPING in CATALINA_OPTS in tomcat8.5, it is not reflected.
An unsupported Java version How to get rid of errors
The permission specification of the FileUtils method is an octal number.
Transition using the header image of CardView as it is