print array in the log cat android [closed]
Asked Answered
H

6

44

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);
     }
 }
Hairtail answered 22/11, 2012 at 12:37 Comment(1)
I Googled this question and this helped me.Dandle
A
145

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]]
Ashley answered 22/11, 2012 at 12:42 Comment(4)
absolutely correct and acceptable answer.Killdeer
@Hairtail 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."Bourque
Not working for two-dimensional array.Floozy
deepToString is working for two-dimensional arrayAshley
K
6

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

for(String log : array)
{
  Log.v("Tag",log);
}
Killdeer answered 22/11, 2012 at 12:42 Comment(0)
T
1

You can use for each loop

for(int x: arr){
Log.d(tag,"x:"+x);
}
Transfinite answered 22/11, 2012 at 12:41 Comment(0)
R
0

Try this way:

for (int i =0 ;i<arr.length;i++)
{
   Log.v("Array Value","Array Value"+arr[i]);
}
Roslynrosmarin answered 22/11, 2012 at 12:41 Comment(0)
H
0

Try this :

for (int i = 0; i < arr.length; i++) {
   Log.d(TAG, arr[i]);
}

What we are doing here is iterating over the array using the for loop to print logcat. Log cat output can be done with Log.d(..), Log.v(..) , Log.i(..) or Log.e(..). See more here.

Hormuz answered 22/11, 2012 at 12:42 Comment(0)
D
0

You can also try plain old

 System.out.println()
Dov answered 22/11, 2012 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.