栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java多维数组到字符串和字符串到数组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java多维数组到字符串和字符串到数组

尝试我的stringToDeep()方法转换回Array。

import java.util.*;public class DeepToArray {public static void main(String[] args) {    int row, col;    row = 2;    col = 3;    String[][] in = new String[row][col];    for (int i = 0; i < row; i++) {        for (int j = 0; j < col; j++) { in[i][j] = i + " " + j;        }    }    String str = Arrays.deepToString(in);    System.out.println(str);    String[][] out = stringToDeep(str);    for (String s2[] : out) {        for (String s3 : s2) { System.out.print(s3 + "  ");        }        System.out.println();    }}private static String[][] stringToDeep(String str) {    int row = 0;    int col = 0;    for (int i = 0; i < str.length(); i++) {        if (str.charAt(i) == '[') { row++;        }    }    row--;    for (int i = 0;; i++) {        if (str.charAt(i) == ',') { col++;        }        if (str.charAt(i) == ']') { break;        }    }    col++;    String[][] out = new String[row][col];    str = str.replaceAll("\[", "").replaceAll("\]", "");    String[] s1 = str.split(", ");    int j = -1;    for (int i = 0; i < s1.length; i++) {        if (i % col == 0) { j++;        }        out[j][i % col] = s1[i];        //System.out.println(s1[i] + "t" + j + "t" + i % col);    }    return out;}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438865.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号