Q re conversion of string to int array

Discussion in 'Java' started by Gerald Rosenberg, Apr 11, 2004.

  1. Given a variable string of the form assigned to myContent, is there a
    different/better approach to converting the string a 2D int array?

    I know I can move the Patterns out and make them static as an
    performance improvement. Having to handle the bounds checking and
    allocation of arrays always seems cumbersome. I do have to return an
    int[][] and am *almost* assured that 9x9 will be large enough. Can you
    suggest the best clean way of doing the checking and allocation?

    Thanks,
    Gerald



    public int[][] runTest() {

    int[][] x = new int[9][9];

    String myContent = "{{1, 3}{26 ,04,35, 99 } { 87, 438,19}}";

    Pattern pattern =
    Pattern.compile("\\{+(\\s*\\d+)(\\s*\\,+\\s*\\d+\\s*)+\\}+");
    Pattern subpattern = Pattern.compile("\\d+");

    Matcher matcher = pattern.matcher(myContent);

    int I = 0;
    int j = 0;
    while (matcher.find()) {
    Matcher submatcher = subpattern.matcher(matcher.group());
    while (submatcher.find()) {
    x[j] = new Integer(submatcher.group()).intValue();
    System.err.println(I + ":" + j + " = " + x[j]);
    j++;
    }
    I++;
    }
    return x;
    }

    Output:
    0:0 = 1
    0:1 = 3
    1:2 = 26
    1:3 = 4
    1:4 = 35
    1:5 = 99
    2:6 = 87
    2:7 = 438
    2:8 = 19
     
    Gerald Rosenberg, Apr 11, 2004
    #1
    1. Advertising

  2. Gerald Rosenberg

    Sudsy Guest

    Gerald Rosenberg wrote:
    > Given a variable string of the form assigned to myContent, is there a
    > different/better approach to converting the string a 2D int array?
    >
    > I know I can move the Patterns out and make them static as an
    > performance improvement. Having to handle the bounds checking and
    > allocation of arrays always seems cumbersome. I do have to return an
    > int[][] and am *almost* assured that 9x9 will be large enough. Can you
    > suggest the best clean way of doing the checking and allocation?
    >
    > Thanks,
    > Gerald
    >
    >
    >
    > public int[][] runTest() {
    >
    > int[][] x = new int[9][9];
    >
    > String myContent = "{{1, 3}{26 ,04,35, 99 } { 87, 438,19}}";
    >
    > Pattern pattern =
    > Pattern.compile("\\{+(\\s*\\d+)(\\s*\\,+\\s*\\d+\\s*)+\\}+");
    > Pattern subpattern = Pattern.compile("\\d+");
    >
    > Matcher matcher = pattern.matcher(myContent);
    >
    > int I = 0;
    > int j = 0;
    > while (matcher.find()) {
    > Matcher submatcher = subpattern.matcher(matcher.group());
    > while (submatcher.find()) {
    > x[j] = new Integer(submatcher.group()).intValue();
    > System.err.println(I + ":" + j + " = " + x[j]);
    > j++;
    > }
    > I++;


    shouldn't you be resetting j to 0 here?

    > }
    > return x;
    > }


    I would use a Vector of Vectors. I fully anticipate that people will
    post saying "that's not very efficient; you should be using...". I'll
    leave that up to you. Here's your modified code:

    Vector I = new Vector();
    int numRows = 0;
    int numCols = 0;
    while (matcher.find()) {
    Vector j = new Vector();
    I.add( j );
    numRows++;
    Matcher submatcher = subpattern.matcher(matcher.group());
    while (submatcher.find()) {
    j.add( new Integer(submatcher.group()) );
    if( j.size() > numCols )
    numCols++;
    }
    }
    int[][] x = new int[numRows][numCols];
    for( int i = 0; i < I.size(); i++ ) {
    Vector row = (Vector) I.elementAt( i );
    for( int j = 0; j < row.size(); j++ )
    x[j] = ( (Integer) row.elementAt( j ) ).intValue();
    }
    return x;
     
    Sudsy, Apr 11, 2004
    #2
    1. Advertising

  3. In article <>,
    says...
    > Gerald Rosenberg wrote:
    > > Given a variable string of the form assigned to myContent, is there a
    > > different/better approach to converting the string a 2D int array?
    > >
    > > I know I can move the Patterns out and make them static as an
    > > performance improvement. Having to handle the bounds checking and
    > > allocation of arrays always seems cumbersome. I do have to return an
    > > int[][] and am *almost* assured that 9x9 will be large enough. Can you
    > > suggest the best clean way of doing the checking and allocation?
    > >
    > > Thanks,
    > > Gerald
    > >
    > >
    > >
    > > public int[][] runTest() {
    > >
    > > int[][] x = new int[9][9];
    > >
    > > String myContent = "{{1, 3}{26 ,04,35, 99 } { 87, 438,19}}";
    > >
    > > Pattern pattern =
    > > Pattern.compile("\\{+(\\s*\\d+)(\\s*\\,+\\s*\\d+\\s*)+\\}+");
    > > Pattern subpattern = Pattern.compile("\\d+");
    > >
    > > Matcher matcher = pattern.matcher(myContent);
    > >
    > > int I = 0;
    > > int j = 0;
    > > while (matcher.find()) {
    > > Matcher submatcher = subpattern.matcher(matcher.group());
    > > while (submatcher.find()) {
    > > x[j] = new Integer(submatcher.group()).intValue();
    > > System.err.println(I + ":" + j + " = " + x[j]);
    > > j++;
    > > }
    > > I++;

    >
    > shouldn't you be resetting j to 0 here?


    Yes. Thanks for spotting that.

    >
    > > }
    > > return x;
    > > }

    >
    > I would use a Vector of Vectors. I fully anticipate that people will
    > post saying "that's not very efficient; you should be using...". I'll
    > leave that up to you. Here's your modified code:
    >
    > Vector I = new Vector();
    > int numRows = 0;
    > int numCols = 0;
    > while (matcher.find()) {
    > Vector j = new Vector();
    > I.add( j );
    > numRows++;
    > Matcher submatcher = subpattern.matcher(matcher.group());
    > while (submatcher.find()) {
    > j.add( new Integer(submatcher.group()) );
    > if( j.size() > numCols )
    > numCols++;
    > }
    > }
    > int[][] x = new int[numRows][numCols];
    > for( int i = 0; i < I.size(); i++ ) {
    > Vector row = (Vector) I.elementAt( i );
    > for( int j = 0; j < row.size(); j++ )
    > x[j] = ( (Integer) row.elementAt( j ) ).intValue();
    > }
    > return x;
    >
    >


    Thanks for the response. I have to admit the perceived inefficiency was
    why I shied from Vector to begin with. And, I thought there must be
    some clean(er) way to do it directly. Not sure if this is any better
    than your proposal.

    Gerald



    public void runTest() {

    String myContent = "{{1, 3, 7}{26,04 ,35, 99 } { 87, 438,19}{99,
    1221,43 ,222, 2,5}{88,77} }";
    int[][] myInt = convert(myContent);
    System.err.println(myInt.toString());
    }

    private static Pattern cut_I = Pattern.compile("\\}+\\s*\\}*");
    private static Pattern pattern = Pattern.compile("(\\{+\\s*\\d+)((\\s*
    \\,\\s*\\d+\\s*)+\\}+\\s*)");

    private static Pattern cut_j = Pattern.compile("\\,");
    private static Pattern subpattern = Pattern.compile("(\\d+)");

    public int[][] convert(String s) {

    int[][] x;

    int gcnt_I = cut_I.split(s).length;
    x = new int[gcnt_I][];

    int I = 0;
    int j = 0;

    Matcher matcher = pattern.matcher(s);
    while (matcher.find()) {
    String stmp = matcher.group();
    int gcnt_j = cut_j.split(stmp).length;
    x = new int[gcnt_j];
    Matcher submatcher = subpattern.matcher(stmp);
    while (submatcher.find()) {
    x[j] = new Integer(submatcher.group()).intValue();
    System.err.println(I + ":" + j + " = " + x[j]);
    j++;
    }
    j = 0;
    I++;
    }
    return x;
    }
     
    Gerald Rosenberg, Apr 11, 2004
    #3
  4. Gerald Rosenberg

    Sudsy Guest

    Gerald Rosenberg wrote:
    <snip>

    Actually, your solution should be more efficient as you're creating
    a "sparse array". Never hurts to throw ideas around...
     
    Sudsy, Apr 11, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S300
    Replies:
    4
    Views:
    22,123
    Roedy Green
    Aug 19, 2003
  2. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  4. arun
    Replies:
    8
    Views:
    482
    Dave Thompson
    Jul 31, 2006
  5. Alain Spineux
    Replies:
    6
    Views:
    655
    Eric Sosman
    May 17, 2011
Loading...

Share This Page