Android : How to determine obj can be used in for loop

on Tuesday, October 28, 2014


I have try following code, but it return false.



package ro.idea.ex;

import java.lang.reflect.Array;
import java.util.Collection;

/**
* Created by roroco on 10/28/14.
*/
public class Ex {
public <T> boolean canWorkInForLoop(T o) {
return o instanceof Collection || o instanceof Iterable || o instanceof Array;
}

public static void main(String[] args) {
int[] arr = {1, 2};
Object r = new Ex().canWorkInForLoop(arr);
System.out.println("r:" + r + "\t\t" + new Exception().getStackTrace()[0].getFileName() + ":" + new Exception().getStackTrace()[0].getLineNumber());
}
}


my question is how to determine all obj which can be used in for loop, like new int[], instance of ArrayList and other obj?


0 comments:

Post a Comment