In this post, we will see how to resolve Convert a String representing a matrix of numbers to a 2D array
Question:
I need to convert a String representing a matrix of numbers to a 2D Array.Best Answer:
This code splits the input string into rows using the newline character \n, and then splits each row into individual numbers using the space character ” “. It then converts each number to an integer using Integer.parseInt() and creates a 2D array.Note that in Java, you need to declare the size of the 2D array before initializing it. Here, we use the length of the rows array to determine the number of rows, and the length of each row to determine the number of columns.
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com