如何看待一个数组中的logcat为Android [英] how see an array in logcat for android

查看:166
本文介绍了如何看待一个数组中的logcat为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录的原始数据就像在我的logcat数组,所以我知道什么是输出。 可以说,我有一个数组...这样的:

I would like to log raw data like arrays in my logcat, so I know what is the output. Lets say I have an array... like that:

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

为什么我不能只是记录阵列来安慰?我该怎么办呢?

Why can't I just log the array to console? How can I do it?

推荐答案

为什么这不工作的原因很简单,因为 Log.v 的第二个参数是一个字符串不是文件[] 。 Java的严格执行参数类型。

The reason why this doesn't work is simply because the 2nd argument of Log.v is a String not a File[]. Java strictly enforces argument types.

更新

您可以轻松地将信息包含在文件对象转换为字符串对象。所有Java对象实施的toString(),而如果我没有记错返回类名和<$的组合C $ C>地址的对象的位置。然而,这通常不包含有用的信息。所以,你需要自己定义的转换。

You can easily transform the information contained a File object into a String object. All java objects implement a toString(), which if I remember correctly returns a combination of the ClassName and the address of the object is located. However this usually doesn't contain useful information. So you need to define the conversion yourself.

现在,从转换文件[] 字符串更复杂,因为当一个阵列上调用方法它的工作原理对数组对象,而不是一个数组(其中包含您所关心的信息,我怀疑)的成员。因此调用 mp3List.toString()将返回描述包含数组中的数组对象,而不是信息的一个字符串。

Now to convert from File[] to String is more complicated because you when call a method on an array it works on the array object rather than on members of an array (which contains the information I suspect you care about). So calling mp3List.toString() will return a single string that describes the array object and not the information contained in the array.

所以,你可能会想写一个方法是这样的:

So you'll probably 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;
}

然后打电话让你的通话记录如下:

And then call make your log call as follows:

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

不过,这可能是难以阅读。

However this might be hard to read.

我个人会做这样的:

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

它的简单,生产更清洁的日志信息,并更容易理解正在发生的事情作为一个程序员。

Its simpler, produces cleaner log messages and is easier to understand what is going on as a programmer.

这篇关于如何看待一个数组中的logcat为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆