Java 将 List<String> 打印到 logcat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22385876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:27:28  来源:igfitidea点击:

Print a List<String> to logcat

javaandroiddebugginglogcatandroid-logcat

提问by johnsonjp34

I can see that the log.d requires

我可以看到 log.d 需要

Log.d(String TAG, String). 

How do I print to the android debug logcat a List String instead of just a String?

如何将列表字符串而不是字符串打印到 android 调试 logcat?

采纳答案by Hamid Shatu

Make use of toString()method which is available for most common data structures:

使用可用于大多数常见数据结构的toString()方法:

Log.d("list", list.toString());

Above statement will give you the expected result if you declare your List/Collectionusing Generic typedefined in Java. Such as String, Integer, Long etc. Cause, they all have implemented toString()method.

如果您使用Java 中定义的泛型类型声明List/ ,则上述语句将为您提供预期的结果。比如String、Integer、Long等。因为,它们都有实现的方法。CollectiontoString()

Custome Generic Type:

客户通用类型:

But if you declare the Listusing your own custom type then you will not get proper output by just calling list.toString(). You need to implement toString()method for your custom type to get expected output.

但是,如果您List使用自己的自定义类型声明 ,那么仅通过调用list.toString(). 您需要toString()为您的自定义类型实现方法以获得预期的输出。

For example:

例如:

You have a model class named Dogas below

您有一个Dog如下命名的模型类

public class Dog{
   String breed;
   int ageC
   String color; 
}

You declared a Listusing Dogtype

你声明了一个ListusingDog类型

List<Dog> dogList = new ArrayList<Dog>();

Now, if you want to print this List in LogCatproperly then you need to implement toString()method in Dogclass.

现在,如果您想LogCat正确打印此列表,则需要toString()Dog类中实现方法。

public class Dog{
   String breed;
   int age
   String color;

   String toString(){
       return "Breed : " + breed + "\nAge : " + age + "\nColor : " + color;
   } 
}

Now, you will get proper result if you call list.toString().

现在,如果您调用 ,您将获得正确的结果list.toString()

回答by Shaikh Mohib

if (list != null && list.size() > 0)
                {
                    for (int i = 0; i < list.size(); i++) {
                        mStrp = mStrp + list.get(i).getdataname() + "/";
                        mStrD  = mStrD + list.get(i).getdata2name() + "/";
                    }

                    //if you want to delete last "/"
                mStrp = mStrLatPick.substring(0,mStrLatPick.length() - 1);

                    value = mStrp + mStrD;
                    Log.d("value",value);
                }

回答by TEJVEER SINGH

If you want to print the Array List element on the console.and also add , remove an element.

如果要在控制台上打印 Array List 元素。还要添加、删除元素。

    ArrayList myFavAnimal = new ArrayList();

    myFavAnimal.add("Lion");
    myFavAnimal.add("Tiger");
    myFavAnimal.add("Leopard");
    Log.i("MyTag",myFavAnimal.get(0) +"");
    myFavAnimal.remove(1);
    Log.i("MyTag",myFavAnimal.toString());
    myFavAnimal.remove("Lion");
    Log.i("MyTag",myFavAnimal.toString());