Q re conversion of string to int array

G

Gerald Rosenberg

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
 
S

Sudsy

Gerald said:
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;
 
G

Gerald Rosenberg

Gerald said:
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;
}
 
S

Sudsy

Gerald Rosenberg wrote:
<snip>

Actually, your solution should be more efficient as you're creating
a "sparse array". Never hurts to throw ideas around...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top