这个 setEmptyValue(字符序列emptyValue) 属于 细木工 设置在确定此StringJoiner的字符串表示形式时要使用的字符序列,并且尚未添加任何元素,即当它为空时。为此,制作了emptyValue参数的副本。请注意,一旦调用了add方法,StringJoiner就不再被认为是空的,即使添加的元素对应于空字符串。
null
语法:
public StringJoiner setEmptyValue(CharSequence emptyValue)
参数: 此方法接受一个强制参数 空值 作为空StringJoiner的值返回的字符
返回: 这个方法返回这个StringJoiner本身,因此调用可能是链接的
例外情况: 这个方法抛出 空指针异常 当emptyValue参数为空时
下面的示例演示了setEmptyValue()方法:
例1:
// Java program to demonstrate // setEmptyValue() method of StringJoiner import java.util.StringJoiner; public class GFG { public static void main(String[] args) { // Create a StringJoiner StringJoiner str = new StringJoiner( " " ); // Print the empty StringJoiner System.out.println( "Initial StringJoiner: " + str); // Add an emptyValue // using setEmptyValue() method str.setEmptyValue( "StrigJoiner is empty" ); // Print the StringJoiner System.out.println( "After setEmptyValue(): " + str); // Add elements to StringJoiner str.add( "Geeks" ); str.add( "forGeeks" ); // Print the StringJoiner System.out.println( "Final StringJoiner: " + str); } } |
输出:
Initial StringJoiner: After setEmptyValue(): StrigJoiner is empty Final StringJoiner: Geeks forGeeks
例2: 演示NullPointerException
// Java program to demonstrate // setEmptyValue() method of StringJoiner import java.util.StringJoiner; public class GFG { public static void main(String[] args) { // Create a StringJoiner StringJoiner str = new StringJoiner( " " ); // Print the empty StringJoiner System.out.println( "Initial StringJoiner: " + str); try { // Add a null emptyValue // using setEmptyValue() method str.setEmptyValue( null ); } catch (Exception e) { System.out.println( "Exception when adding null" + " in setEmptyValue(): " + e); } } } |
输出:
Initial StringJoiner: Exception when adding null in setEmptyValue(): java.lang.NullPointerException: The empty value must not be null
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END