Java 程序,将一维和多维数组转换为字符串


为了将数组转换为一维或二维数组,我们先创建一个一维和二维数组−

一维

String str[] = {"p", "q", "r", "s", "t", "u", "v", "w","x", "y", "z"};

二维

doubled [][]= {
   {1.2, 1.3, 2.1, 4.1},
   {1.5, 2.3},
   {2.5, 4.4},
   {3.8},
   {4.9},
   {3.2, 2.1, 3.2, 7.2}
};

将一维数组转换为字符串 −

Arrays.toString(str);

将二维数组转换为字符串 −

Arrays.deepToString(d);

范例

import java.util.Arrays;
public class Demo {
   public static void main(String args[]) {
      String str[] = {"p", "q", "r", "s", "t", "u", "v", "w","x", "y", "z"};
      doubled [][]= {
         {1.2, 1.3, 2.1, 4.1},
         {1.5, 2.3},
         {2.5, 4.4},
         {3.8},
         {4.9},
         {3.2, 2.1, 3.2, 7.2}
      };
      System.out.println("One dimensional = "+Arrays.toString(str));
      System.out.println("Two dimensional = "+Arrays.deepToString(d));
   }
}

输出

One dimensional = [p, q, r, s, t, u, v, w, x, y, z]
Two dimensional = [[1.2, 1.3, 2.1, 4.1], [1.5, 2.3], [2.5, 4.4], [3.8], [4.9], [3.2, 2.1, 3.2, 7.2]]

更新于:30-7-2019

304 次浏览

启动您的 职业生涯

完成课程后获取认证

开始
广告