print array in the log cat android

61,627

Solution 1

You can use Arrays.toString

Log.d("this is my array", "arr: " + Arrays.toString(arr));
// or
System.out.println("arr: " + Arrays.toString(arr));

Or, if your array is multidimensional, use Arrays.deepToString()

String[][] x = new String[][] {
    new String[] { "foo", "bar" },
    new String[] { "bazz" }
};
Log.d("this is my deep array", "deep arr: " + Arrays.deepToString(x));
// or
System.out.println("deep arr: " + Arrays.deepToString(x));
// will output: [[foo, bar], [bazz]]

Solution 2

Very simple use for each loop much fast then normal for (incremental) loop.

for(String log : array)
{
  Log.v("Tag",log);
}

Solution 3

You can use for each loop

for(int x: arr){
Log.d(tag,"x:"+x);
}
Share:
61,627
user1760556
Author by

user1760556

Updated on November 09, 2020

Comments

  • user1760556
    user1760556 over 3 years

    How can I print the arr variable in the log to see the results of the array thanks,

     public void onClick(View v) {
         if(v.getId()==R.id.buttonone)
         {
              genrandom grandom =new genrandom();
              int[] arr=new int[50];
              arr = new  gen_random_number().genrandom(arr, yourXvalue);
         }
     }
    
  • Mohd Mufiz
    Mohd Mufiz over 11 years
    absolutely correct and acceptable answer.
  • assylias
    assylias over 11 years
    @user1760556 tag can be any string you want. From the javadoc: "Used to identify the source of a log message. It usually identifies the class or activity where the log call occurs."
  • Deepak
    Deepak over 10 years
    Not working for two-dimensional array.
  • Alex
    Alex over 10 years
    deepToString is working for two-dimensional array