简体   繁体   English

如何在logcat中看到一个用于android的数组

[英]how see an array in logcat for android

I would like to log raw data like arrays in my logcat, so I know what is the output. 我想在我的logcat中记录像数组这样的原始数据,所以我知道输出是什么。 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? 我该怎么做?

You can't log the array because it is just an Object. 您无法记录该数组,因为它只是一个Object。 LogCat has no idea how to deal with it or display it in a way you want. LogCat不知道如何处理它或以你想要的方式显示它。

If each File object has a toString() method that displays the information that you want you can use: 如果每个File对象都有一个toString()方法,它显示了您可以使用的信息:

Log.v(Arrays.toString(mp3List));

Otherwise you'll have to concatenate your own String to log: 否则,您必须将自己的String连接到日志:

StringBuilder sb = new StringBuilder();
for(File f : mp3List) {
  sb.append(f.getName());
}

Log.v(sb.toString());

The reason why this doesn't work is simply because the 2nd argument of Log.v is a String not a File[] . 这不起作用的原因仅仅是因为Log.v的第二个参数是String而不是File[] Java strictly enforces argument types. Java严格执行参数类型。

Update : 更新

You can easily transform the information contained a File object into a String object. 您可以轻松地将包含File对象的信息转换为String对象。 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. 所有java对象都实现了一个toString() ,如果我没记错的话,返回ClassName的组合和对象的address However this usually doesn't contain useful information. 但是,这通常不包含有用的信息。 So you need to define the conversion yourself. 所以你需要自己定义转换。

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). 现在要从File[]转换为String更复杂,因为当你在数组上调用一个方法时,它可以在数组对象上工作,而不是在数组的成员上(包含我怀疑你关心的信息)。 So calling mp3List.toString() will return a single string that describes the array object and not the information contained in the array. 因此,调用mp3List.toString()将返回描述数组对象的单个字符串,而不是数组中包含的信息。

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. 然而,这可能很难阅读。

I personally would do it like this: 我个人会这样做:

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. 它更简单,可以生成更清晰的日志消息,并且更容易理解程序员正在发生的事情。

If you have a String array, you can just add toString() to it and it will be displayed. 如果你有一个String数组,你可以只添加toString()并显示它。

For custom objects, you should override the toString() method and print what you want to see for that object. 对于自定义对象,您应该覆盖toString()方法并打印要为该对象查看的内容。 If you then have an array, the array will be printed with the output from the toString method. 如果您有一个数组,则将使用toString方法的输出打印该数组。

我更喜欢一个衬垫:

for(File file:list) Log.d(TAG, "list: " + file.getPath());

Maybe I misunderstood, but I think its simply: 也许我误解了,但我认为它很简单:

string sLog = "";
for(mp3List..)
{
    sLog += mp3List[i].getName();
}

Log.v(sLog);

isnT it? 不是吗? Because, I suppose, when you try to print an array like that you ll get a log entry saying that mp3List is an System.Blah.Blah.File[] .. 因为,我想,当你尝试打印这样的数组时,你会得到一个日志条目,说mp3List是一个System.Blah.Blah.File[] ..

Hope it helps.. 希望能帮助到你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM