Java中的 AtomicReferenceArray toString() 方法及示例
AtomicReferenceArray类的toString()方法用于返回数组当前值的 String 表示。该方法用于将 AtomicReferenceArray 的内容表示为字符串
句法:
public String toString()
参数:此方法不接受任何内容。
返回值:此方法返回数组当前值的字符串表示形式。
下面的程序说明了 toString() 方法:
方案一:
// Java program to demonstrate
// toString() method
import java.util.concurrent.atomic.*;
public class GFG {
public static void main(String[] args)
{
// create an atomic reference object.
AtomicReferenceArray ref
= new AtomicReferenceArray(5);
// set some value
ref.set(0, 234);
ref.set(1, 134);
ref.set(2, 325);
// print the toString() return
String toString = ref.toString();
System.out.println(
"String representation of"
+ " AtomicReferenceArray:\n"
+ toString);
}
}
输出:
String representation of AtomicReferenceArray:
[234, 134, 325, null, null]
方案二:
// Java program to demonstrate
// toString() method
import java.util.concurrent.atomic.*;
public class GFG {
public static void main(String[] args)
{
// create a array of Strings
String[] names
= { "AMAN", "AMAR", "SURAJ" };
// create an atomic reference object.
AtomicReferenceArray ref
= new AtomicReferenceArray(names);
// print the toString() return
String toString = ref.toString();
System.out.println(
"String representation of"
+ " AtomicReferenceArray:\n"
+ toString);
}
}
输出:
String representation of AtomicReferenceArray:
[AMAN, AMAR, SURAJ]
参考资料: https: Java/util/concurrent/atomic/AtomicReferenceArray.html#toString()