Java中的 ChoiceFormat getFormats() 方法及示例
Java.text.ChoiceFormat类的getFormats()方法用于在初始化时获取ChoiceFormat 对象的附加格式。它提供指定类型的数组。
句法:
public Object[] getFormats()
参数:此方法不接受任何参数。
返回值:此方法返回一个指定类型的数组,该数组是附加到 ChoiceFormat 对象的格式。
以下是说明getFormats()方法的示例:
示例 1:
Java
// Java program to demonstrate
// getFormats() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing ChoiceFormat
ChoiceFormat cf1
= new ChoiceFormat(
"4#wed| 5#thu | 6#fri | 7#sat");
// getting format attached
// to the ChoiceFormat
// using getFormats() method
String[] format
= (String[])cf1.getFormats();
// display the result
System.out.print("Format: "
+ Arrays.toString(format));
}
}
Java
// Java program to demonstrate
// getFormats() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing limit
double[] limit = { 1, 2, 3, 4 };
// creating and initializing format
String[] format
= { "add", "sub",
"multiply", "divide" };
// creating and initializing ChoiceFormat
ChoiceFormat cf
= new ChoiceFormat(limit, format);
// getting format attached
// to the ChoiceFormat
// using getFormats() method
String[] format
= (String[])cf.getFormats();
// display the result
System.out.print("Format: "
+ Arrays.toString(format));
}
}
输出:
Format: [wed, thu , fri , sat]
示例 2:
Java
// Java program to demonstrate
// getFormats() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing limit
double[] limit = { 1, 2, 3, 4 };
// creating and initializing format
String[] format
= { "add", "sub",
"multiply", "divide" };
// creating and initializing ChoiceFormat
ChoiceFormat cf
= new ChoiceFormat(limit, format);
// getting format attached
// to the ChoiceFormat
// using getFormats() method
String[] format
= (String[])cf.getFormats();
// display the result
System.out.print("Format: "
+ Arrays.toString(format));
}
}
输出:
Format: [add, sub, multiply, divide]
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/ChoiceFormat.html#getFormats–