How to convert a specific row of a decimal matrix to a string in Java

1 Answer

0 votes
import java.io.StringWriter;
   
public class ConvertSpecificRowOfMatrixToString_Java {
    public static String convertRowOfMatrixToString(int[][] matrix, int row) {
        int cols = matrix[0].length;
        StringWriter sw = new StringWriter();
          
        for (int j = 0; j < cols; j++) {
            sw.write(String.valueOf(matrix[row][j]));
            sw.write(" ");
        }
          
        return sw.toString().trim();
    }
   
    public static void main(String[] args) {
        int[][] matrix = {
                {4, 7, 9, 18, 29, 0},
                {1, 9, 18, 99, 4, 3},
                {9, 17, 89, 2, 7, 5},
                {19, 49, 6, 1, 9, 8},
                {29, 4, 7, 9, 18, 6}
        };
   
        int row = 2;
  
        String str = convertRowOfMatrixToString(matrix, row);
           
        System.out.println(str);
    }
}
 
   
   
/*
run:
   
9 17 89 2 7 5
   
*/

 



answered Jul 11, 2024 by avibootz
edited Jul 13, 2024 by avibootz

Related questions

...