I've got a logger which accepts object as input, and it is rather convenient for me. But now I've got to log an input which is of type int[]
. I want to modify the method so it properly logs any array or Iterable.
In c#, what I would do is this:
int[]data=new int[10];
object tmp=data;
.....
if (tmp is IEnumerable)
{
StringBuilder _tmp = new StringBuilder();
foreach (var i in (IEnumerable)tmp)
{
if (_tmp.Length > 0)
{
_tmp.Append(",");
}
_tmp.Append(i);
}
var r = _tmp.ToString();
}
I've tried checking int[]
against Iterable
(it's not), than I tried checking with tmp.getClass().isArray()
, which works, but now I have trouble enumerating the members of that array. I need a method that will work with any array or list of objects.
How do I properly do this in Java?
You could use the methods in
java.lang.reflect.Array
likeOutput is
1, 2, 3, 4