DEV Community

Lalit Kumar
Lalit Kumar

Posted on

java convert string array to int array

When we are handling matrices in Java we can dump the contents of the matrix in a simple way on the screen. But if we just want to see the content of the array we can convert the array to a string with Java.


title: "java convert string array to int array"
tags: java

canonical_url: https://kodlogs.com/blog/2427/java-convert-string-array-to-int-array

The first thing will be to define our matrix:

int [] [] matrix = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};

Now in order to convert an array into a string with Java, we could think that it will be worth it simply by invoking the .toString ()

System.out.println (array.toString ());

And we have it all ... But wait a minute. On the screen it shows me the following:

[[I @ 15db9742

This is little like a matrix to us. So the .toString () method does not work for us to convert array to string with Java.

What happens to us here is something similar to what happened when we compared arrays using the .equals () method. And here we have a similar solution. And it is that the Arrays class offers us a recursive method called .deepToString. The .deepToString method will recursively loop through the array and convert it to a string.

In this way we will have to code it as follows:

System.out.println (Arrays.deepToString (array));

And so we will get on the screen:

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

And we will have already managed to convert the matrix to string with Java in a simple way.

Top comments (0)