आम पूर्वज कोड करने के लिए और boolean[]
, Object
है, इसलिए जब तक आप allTrue(Object booleans)
साथ ठीक हैं, तो आप एक विधि के साथ यह नहीं कर सकते।
यदि आप अपना विधि हस्ताक्षर allTrue(Iterable<Boolean> booleans)
पर बदलते हैं, तो आपको केवल बूलियन सरणी को पार करने के लिए एक विशेष Iterator<Boolean>
बनाना है।
import java.util.Iterator;
import java.util.NoSuchElementException;
public class BooleanAllTrue {
public static boolean allTrue(Iterable<Boolean> booleans) {
if (booleans == null) return false;
for (Boolean bool : booleans) {
if (!bool) return false;
}
return true;
}
public static Iterable<Boolean> asIterable(final boolean[] booleens) {
return new Iterable<Boolean>() {
public Iterator<Boolean> iterator() {
final boolean[] booleans = booleens;
return new Iterator<Boolean>() {
private int i = 0;
public boolean hasNext() {
return i < booleans.length;
}
public Boolean next() {
if (!hasNext()) throw new NoSuchElementException();
return booleans[i++];
}
public void remove() {throw new UnsupportedOperationException("remove");}
};
}
};
}
public static void main(String [] args) {
System.out.println(allTrue(asIterable(new boolean[]{true, true})));
System.out.println(allTrue(asIterable(new boolean[]{true, false})));
try {
asIterable(new boolean[0]).iterator().next();
} catch (NoSuchElementException e) {
// expected
}
}
}
और अंत में allTrue(boolean[] booleans)
विधि।
public static boolean allTrue(boolean[] booleans) {
return allTrue(asIterable(booleans));
}
स्रोत
2017-02-10 03:14:22
क्या आप किसी भी मौके से अमरूद का उपयोग कर रहे हैं? – shmosel
देखें http://stackoverflow.com/a/5606435/2310289 –
@shmosel नो गुवा – Kwoppy