Java – how to see an array in Android’s logcat

I want to record arrays in my logcat, such as arrays, so I know what the output is

File[] mp3List = ...
Log.v("test",mp3List);

Why can't I log the array to the console? What shall I do?

Solution

The reason why this doesn't work is just because of log The second parameter of V is string instead of file [] Java strictly enforces parameter types

to update:

You can easily convert information containing file objects into string objects All Java objects implement a toString (), if I remember correctly returning the combination of classname and the address of the object However, this usually does not contain useful information So you need to define the transformation yourself

Now converting from file [] to string is more complicated because when you call a method on an array, it can work on an array object rather than array members (containing information I doubt you care about) So call mp3list Tostring() will return a string describing the array object instead of the information contained in the array

So you might want to write a method like this:

String fileArrayToString(File[] f){
    String output = "";
    String delimiter = "\n" // Can be new line \n tab \t etc...
    for (int i=0; i<f.length; i++)
    {
        output = output + f[i].getPath() + delimiter;
    }

    return output;
}

Then call your log to call as follows:

Log.v("mytag",fileArraytoString(mp3List);

But it can be difficult to read

I personally do this:

for (int i=0; i<mp3List.legnth; i++)
    Log.v("mytag",Integer.toString(i) + ":" + mp3List[i].getPath());

It is simpler, generates cleaner log messages, and is easier to understand as a programmer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>