Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

Java: แปลง Array เป็น String ด้วย Arrays.deepToString()

เมธอด Arrays.deepToString() ใช้สำหรับแปลง array (รวม nested/multi-dimensional arrays) เป็น String representation ที่อ่านง่าย เหมาะสำหรับ debugging และ logging

Syntax

public static String deepToString(Object[] a)
  • รับ Object[] (ทำงานกับ array ของ objects และ nested arrays)
  • คืนค่า String ในรูปแบบ [element1, element2, ...]
  • สำหรับ primitive arrays ต้องใช้ wrapper arrays หรือ Arrays.toString() แทน

ตัวอย่างที่ 1: 1-D Object Array

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
        Object[] ob = {"One", "Two", "Three", "Four"};

        System.out.println("Array elements...");
        for (Object value : ob) {
            System.out.println("Value = " + value);
        }

        System.out.println("The string representation of array is:");
        System.out.println(Arrays.deepToString(ob));
    }
}

Output

Array elements...
Value = One
Value = Two
Value = Three
Value = Four
The string representation of array is:
[One, Two, Three, Four]

ตัวอย่างที่ 2: 2-D Array (Nested Arrays)

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
        int[][] arr = new int[3][3];
        arr[0][0] = 10;  arr[0][1] = 20;  arr[0][2] = 30;
        arr[1][0] = 40;  arr[1][1] = 50;  arr[1][2] = 75;
        arr[2][0] = 100; arr[2][1] = 150; arr[2][2] = 200;

        System.out.println(Arrays.deepToString(arr));
    }
}

Output

[[10, 20, 30], [40, 50, 75], [100, 150, 200]]

ตัวอย่างที่ 3: Mixed Nested Arrays

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
        Object[] mixed = {
            "String",
            42,
            new int[]{1, 2, 3},
            new String[]{"a", "b", "c"},
            new Object[]{true, 3.14, "nested"}
        };

        System.out.println(Arrays.deepToString(mixed));
    }
}

Output

[String, 42, [1, 2, 3], [a, b, c], [true, 3.14, nested]]

Arrays.toString() vs Arrays.deepToString()

เมธอด ใช้กับ Nested Arrays ตัวอย่าง Output
Arrays.toString(int[]) Primitive 1-D arrays ไม่รองรับ (พิมพ์ address) [1, 2, 3]
Arrays.toString(Object[]) Object 1-D arrays ไม่รองรับ (พิมพ์ address ของ nested array) [1, 2, [I@15db9742]
Arrays.deepToString(Object[]) Object arrays (รวม nested) รองรับครบ (recursive) [1, 2, [3, 4]]

กรณีใช้งาน Primitive Multi-dimensional Arrays

เนื่องจาก deepToString รับ Object[] เท่านั้น สำหรับ int[][] ต้อง cast หรือใช้ Stream:

int[][] matrix = {{1,2}, {3,4}};

// วิธี 1: Cast to Object[]
System.out.println(Arrays.deepToString((Object[]) matrix));

// วิธี 2: Java 8+ Stream
String result = Arrays.stream(matrix)
    .map(Arrays::toString)
    .collect(Collectors.joining(", ", "[", "]"));
System.out.println(result); // [[1, 2], [3, 4]]

ข้อจำกัด

  • Circular reference จะโยน StackOverflowError (ไม่มี cycle detection)
  • Null array คืนค่า "null"
  • Null element ใน array คืนค่า "null" ใน string
  • Performance: ช้ากว่า toString เนื่องจาก recursive calls