当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AtomicReferenceArray toString()用法及代码示例


AtomicReferenceArray类的toString()方法用于返回数组当前值的String表示形式,该方法用于将AtomicReferenceArray的内容表示为字符串

用法:

public String toString()

参数:此方法不接受任何内容。


返回值:此方法返回数组当前值的String表示形式。

以下示例程序旨在说明toString()方法:
程序1:

// 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<Integer> ref 
            = new AtomicReferenceArray<Integer>(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]

程序2:

// 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<String> ref 
            = new AtomicReferenceArray<String>(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://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#toString()



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 AtomicReferenceArray toString() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。