Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

add main

By: a guest on Jun 23rd, 2010  |  syntax: Java  |  size: 1.17 KB  |  views: 98  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     package main;
  2.  
  3.     import java.util.Arrays;
  4.     import java.util.List;
  5.     import java.util.ArrayList;
  6.  
  7.     class ParseArray {
  8.         public static void main( String [] args ){
  9.             String input = "[    [ 1] , [2,3],[4, 5,6 , 7],[8,9,10    ,  11,      12,    13]        ]";
  10.            
  11.             List<String[]> multiDimArray = new ArrayList<String[]>();
  12.            
  13.             String [] parts = input
  14.                 .replaceAll("\\s*\\[\\s*","[") // clean starting spaces
  15.                 .replaceAll("\\s*,\\s*", ",")  // middle spaces
  16.                 .replaceAll("\\s*\\]\\s*","]") // ending spaces
  17.                 .replaceAll("\\[\\[|\\]\\]","") // and remove leading an trailing [['s and ]]'s
  18.                
  19.                 .split("\\],\\[");  // finally split by ],[
  20.  
  21.             for( String s : parts ){
  22.                 multiDimArray.add(  s.split(",") ) ;
  23.             }
  24.            
  25.             String [][] result = multiDimArray.toArray( new String[multiDimArray.size()][]);
  26.             print( result );
  27.         }
  28.         public static void print( String[][] array ){
  29.             System.out.println( Arrays.deepToString( array ));
  30.         }
  31.     }