Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am combining two matrices:

matrixA =

719.0 501.0 -75.0
501.0 508.0 -62.0
-75.0 -62.0 10.0

matrixB =

-19.0 -19.0 -19.0 -19.0 -19.0 -19.0 -19.0 -19.0 -19.0 -19.0
-20.0 -20.0 -20.0 -20.0 -20.0 -20.0 -20.0 -20.0 -20.0 -20.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

matrixA#matrixB- combines using # and

the row is separated using , and

element separated using |

My toString code is:

public String toString() {
        String separator = "|";
            StringBuffer result = new StringBuffer();

        for (int k = 0; k < keys.length; k++) {
                 for(int l = 0; l < keys[k].length; l++){
                result.append(keys[k][l]);
                result.append(separator);
            }
           result.setLength(result.length() - separator.length());
            // add a line break.
            result.append(",");
        }
       result.append("#");
        for (int i = 0; i < values.length; i++) {
           for(int j = 0; j < values[i].length; j++){
                result.append(values[i][j]);
                result.append(separator);
            }
            // remove  separator
            result.setLength(result.length() - separator.length());
            // add a line break.
            result.append(",");
        }
      return result.toString();
    }

and my result is:

719.0|501.0|-75.0,501.0|508.0|-62.0,-75.0|-62.0|10.0,#-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0|-19.0,-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0|-20.0,0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0.0,

How to remove the last separator?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could simply remove it by getting a substring without the last element.

return result.substring(0, result.length() - 1);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...