Java将1D数组转换为2D

时间:2013-12-05 02:18:44

标签: java arrays

我真的不了解2D数组...

我有这个1D数组,我想把它转换为2D,所以数字在一个维度,而描述在另一个维度。

这是我目前的数组:

String[] errorcodes = {
        "1|Failed to set a UBC parameter",
        "2|Failed to set a fair scheduler parameter",
        "3|Generic system error",
        "5|The running kernel is not an OpenVZ kernel (or some OpenVZ modules are not loaded)",
        "6|Not enough system resources",
        "7|ENV_CREATE ioctl failed",
        "8|Command executed by vzctl exec returned non-zero exit code",
        "9|Container is locked by another vzctl invocation",
        "10|Global OpenVZ configuration file vz.conf(5) not found",
        "11|A vzctl helper script file not found",
        "12|Permission denied",
        "13|Capability setting failed",
        "14|Container configuration file ctid.conf(5) not found",
        "15|Timeout on vzctl exec",
        "16|Error during vzctl suspend",
        "17|Error during vzctl resume",
        "18|Error from setluid() syscall",
        "20|Invalid command line parameter",
        "21|Invalid value for command line parameter",
        "22|Container root directory (VE_ROOT) not set",
        "23|Container private directory (VE_PRIVATE) not set",
        "24|Container template directory (TEMPLATE) not set",
        "28|Not all required UBC parameters are set, unable to start container",
        "29|OS template is not specified, unable to create container",
        "31|Container not running",
        "32|Container already running",
        "33|Unable to stop container",
        "34|Unable to add IP address to container",
        "40|Container not mounted",
        "41|Container already mounted",
        "43|Container private area not found",
        "44|Container private area already exists",
        "46|Not enough disk space",
        "47|Bad/broken container (/sbin/init or /bin/sh not found)",
        "48|Unable to create a new container private area",
        "49|Unable to create a new container root area",
        "50|Unable to mount container",
        "51|Unable to unmount container",
        "52|Unable to delete a container",
        "53|Container private area not exist",
        "60|vzquota on failed",
        "61|vzquota init failed",
        "62|vzquota setlimit failed",
        "63|Parameter DISKSPACE not set",
        "64|Parameter DISKINODES not set",
        "65|Error setting in-container disk quotas",
        "66|vzquota off failed",
        "67|ugid quota not initialized",
        "71|Incorrect IP address format",
        "74|Error changing password",
        "78|IP address already in use",
        "79|Container action script returned an error",
        "82|Config file copying error",
        "86|Error setting devices (--devices or --devnodes)",
        "89|IP address not available",
        "91|OS template not found",
        "99|Ploop is not supported by either the running kernel or vzctl.",
        "100|Unable to find container IP address",
        "104|VE_NETDEV ioctl error", "105|Container start disabled",
        "106|Unable to set iptables on a running container",
        "107|Distribution-specific configuration file not found",
        "109|Unable to apply a config",
        "129|Unable to set meminfo parameter",
        "130|Error setting veth interface",
        "131|Error setting container name",
        "133|Waiting for container start failed",
        "139|Error saving container configuration file",
        "148|Error setting container IO parameters (ioprio)",
        "150|Ploop image file not found",
        "151|Error creating ploop image",
        "152|Error mounting ploop image",
        "153|Error unmounting ploop image",
        "154|Error resizing ploop image",
        "155|Error converting container to ploop layout",
        "156|Error creating ploop snapshot",
        "157|Error merging ploop snapshot",
        "158|Error deleting ploop snapshot",
        "159|Error switching ploop snapshot",
        "166|Error compacting ploop image",
        "167|Error listing ploop snapsots", };

我希望能够在一行中完成,就像这样。

4 个答案:

答案 0 :(得分:1)

为此你应该使用HashMap

答案 1 :(得分:0)

您可以根据现有的errorcodes

进行操作
String[][] errorcodesArray = new String[errorcodes.length][2];
for (int i = 0; i < errorcodes.length; i++) {
  StringTokenizer st = new StringTokenizer(
      errorcodes[i], "|");
  if (st.hasMoreTokens())
    errorcodesArray[i][0] = st.nextToken();
  if (st.hasMoreTokens())
    errorcodesArray[i][1] = st.nextToken();
}
for (int i = 0; i < errorcodesArray.length; i++) {
  System.out.println(Arrays
      .toString(errorcodesArray[i]));
}

或您的代码单独执行

String[][] errorcodesArray = {
  { "1", "Failed to set a UBC parameter"},
  { "2", "Failed to set a fair scheduler parameter"},
  { "3", "Generic system error"},
  { "5", "The running kernel is not an OpenVZ kernel (or some OpenVZ modules are not loaded)"},
  { "6", "Not enough system resources"},
  { "7", "ENV_CREATE ioctl failed"},
  { "8", "Command executed by vzctl exec returned non-zero exit code"},
  { "9", "Container is locked by another vzctl invocation"},
  { "10", "Global OpenVZ configuration file vz.conf(5) not found"},
  { "11", "A vzctl helper script file not found"},
  { "12", "Permission denied"},
  { "13", "Capability setting failed"},
  { "14", "Container configuration file ctid.conf(5) not found"},
  { "15", "Timeout on vzctl exec"},
  { "16", "Error during vzctl suspend"},
  { "17", "Error during vzctl resume"},
  { "18", "Error from setluid() syscall"},
  { "20", "Invalid command line parameter"},
  { "21", "Invalid value for command line parameter"},
  { "22", "Container root directory (VE_ROOT) not set"},
  { "23", "Container private directory (VE_PRIVATE) not set"},
  { "24", "Container template directory (TEMPLATE) not set"},
  { "28", "Not all required UBC parameters are set, unable to start container"},
  { "29", "OS template is not specified, unable to create container"},
  { "31", "Container not running"},
  { "32", "Container already running"},
  { "33", "Unable to stop container"},
  { "34", "Unable to add IP address to container"},
  { "40", "Container not mounted"},
  { "41", "Container already mounted"},
  { "43", "Container private area not found"},
  { "44", "Container private area already exists"},
  { "46", "Not enough disk space"},
  { "47", "Bad/broken container (/sbin/init or /bin/sh not found)"},
  { "48", "Unable to create a new container private area"},
  { "49", "Unable to create a new container root area"},
  { "50", "Unable to mount container"},
  { "51", "Unable to unmount container"},
  { "52", "Unable to delete a container"},
  { "53", "Container private area not exist"},
  { "60", "vzquota on failed"},
  { "61", "vzquota init failed"},
  { "62", "vzquota setlimit failed"},
  { "63", "Parameter DISKSPACE not set"},
  { "64", "Parameter DISKINODES not set"},
  { "65", "Error setting in-container disk quotas"},
  { "66", "vzquota off failed"},
  { "67", "ugid quota not initialized"},
  { "71", "Incorrect IP address format"},
  { "74", "Error changing password"},
  { "78", "IP address already in use"},
  { "79", "Container action script returned an error"},
  { "82", "Config file copying error"},
  { "86", "Error setting devices (--devices or --devnodes)"},
  { "89", "IP address not available"},
  { "91", "OS template not found"},
  { "99", "Ploop is not supported by either the running kernel or vzctl."},
  { "100", "Unable to find container IP address"},
  { "104", "VE_NETDEV ioctl error"},
  { "105", "Container start disabled"},
  { "106", "Unable to set iptables on a running container"},
  { "107", "Distribution-specific configuration file not found"},
  { "109", "Unable to apply a config"},
  { "129", "Unable to set meminfo parameter"},
  { "130", "Error setting veth interface"},
  { "131", "Error setting container name"},
  { "133", "Waiting for container start failed"},
  { "139", "Error saving container configuration file"},
  { "148", "Error setting container IO parameters (ioprio)"},
  { "150", "Ploop image file not found"},
  { "151", "Error creating ploop image"},
  { "152", "Error mounting ploop image"},
  { "153", "Error unmounting ploop image"},
  { "154", "Error resizing ploop image"},
  { "155", "Error converting container to ploop layout"},
  { "156", "Error creating ploop snapshot"},
  { "157", "Error merging ploop snapshot"},
  { "158", "Error deleting ploop snapshot"},
  { "159", "Error switching ploop snapshot"},
  { "166", "Error compacting ploop image"},
  { "167", "Error listing ploop snapsots"}
};

生成阵列......

System.out.println("    {");
for (int i = 0; i < errorcodesArray.length; i++) {
  System.out.print("      { \"");
  System.out.print(errorcodesArray[i][0]);
  System.out.print("\", \"");
  System.out.print(errorcodesArray[i][1]);
  System.out.print("\"}");
  if (i + 1 < errorcodesArray.length) {
    System.out.print(",");
  }
  System.out.println();
}
System.out.println("    };");

答案 2 :(得分:0)

我建议您使用 HashMap ,例如 HashMap&lt;整数,字符串&gt; 。关键是错误代码,值是错误消息。

这有助于您为给定的错误代码填充错误消息。如果使用String [] [],则对于给定的错误代码填充错误消息并不容易。

HashMap中==&GT; http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html?is-external=true

如果需要2d数组类型的 String [] [] ,那么您可以尝试使用以下代码将1D转换为2D数组。

    //2D string to be converted.
    String[][] convertedArray = new String[errorcodes.length][];

    for(int i=0, len = convertedArray.length;i<len;i++)
    {
        convertedArray[i] = errorcodes[i].split("\\|");
    }
    //Print the 2D array in console
    System.out.println(Arrays.deepToString(convertedArray));

以示例数据为例,控制台中的输出如下:

[[1, Failed to set a UBC parameter], [2, Failed to set a fair scheduler parameter], [3, Generic system error], [5, The running kernel is not an OpenVZ kernel (or some OpenVZ modules are not loaded)], [6, Not enough system resources], [7, ENV_CREATE ioctl failed], [8, Command executed by vzctl exec returned non-zero exit code], [9, Container is locked by another vzctl invocation], [10, Global OpenVZ configuration file vz.conf(5) not found], [11, A vzctl helper script file not found], [12, Permission denied], [13, Capability setting failed], [14, Container configuration file ctid.conf(5) not found], [15, Timeout on vzctl exec], [16, Error during vzctl suspend], [17, Error during vzctl resume], [18, Error from setluid() syscall], [20, Invalid command line parameter], [21, Invalid value for command line parameter], [22, Container root directory (VE_ROOT) not set], [23, Container private directory (VE_PRIVATE) not set], [24, Container template directory (TEMPLATE) not set], [28, Not all required UBC parameters are set, unable to start container], [29, OS template is not specified, unable to create container], [31, Container not running], [32, Container already running], [33, Unable to stop container], [34, Unable to add IP address to container], [40, Container not mounted], [41, Container already mounted], [43, Container private area not found], [44, Container private area already exists], [46, Not enough disk space], [47, Bad/broken container (/sbin/init or /bin/sh not found)], [48, Unable to create a new container private area], [49, Unable to create a new container root area], [50, Unable to mount container], [51, Unable to unmount container], [52, Unable to delete a container], [53, Container private area not exist], [60, vzquota on failed], [61, vzquota init failed], [62, vzquota setlimit failed], [63, Parameter DISKSPACE not set], [64, Parameter DISKINODES not set], [65, Error setting in-container disk quotas], [66, vzquota off failed], [67, ugid quota not initialized], [71, Incorrect IP address format], [74, Error changing password], [78, IP address already in use], [79, Container action script returned an error], [82, Config file copying error], [86, Error setting devices (--devices or --devnodes)], [89, IP address not available], [91, OS template not found], [99, Ploop is not supported by either the running kernel or vzctl.], [100, Unable to find container IP address], [104, VE_NETDEV ioctl error], [105, Container start disabled], [106, Unable to set iptables on a running container], [107, Distribution-specific configuration file not found], [109, Unable to apply a config], [129, Unable to set meminfo parameter], [130, Error setting veth interface], [131, Error setting container name], [133, Waiting for container start failed], [139, Error saving container configuration file], [148, Error setting container IO parameters (ioprio)], [150, Ploop image file not found], [151, Error creating ploop image], [152, Error mounting ploop image], [153, Error unmounting ploop image], [154, Error resizing ploop image], [155, Error converting container to ploop layout], [156, Error creating ploop snapshot], [157, Error merging ploop snapshot], [158, Error deleting ploop snapshot], [159, Error switching ploop snapshot], [166, Error compacting ploop image], [167, Error listing ploop snapsots]]

答案 3 :(得分:0)

您可以直接从初始化创建2D数组,而不是一起创建1D数组,或者您可以将1D数组转换为2D数组(使用split命令并迭代1D数组):

String String[][] errorCodes = 
                   new String[errorcodes.length][errorcodes[0].split("|").length];

for (int i = 0; i < errorcodes.length; i++) 
    errorCodes[i] = errorcodes[i].split("|");

出于某种原因,这不起作用。它给出了奇怪的输出。我之前已经完成了这项工作,但它已经奏效了。所以这个想法就在那里。