2
I have this code in Long
functioning normally:
public static void main(String args[]){
List<Long> lista = new ArrayList();
for(long a = 1; a <= 100; a++){
if(a%2==0) {}
else if (a%3==0){}
else {
lista.add(a);
}
}
System.out.println(lista);
}
}
I made this other code in BigInteger
for the same function, but does not work:
public static void main(String[] args) {
List<BigInteger> lista = new ArrayList();
BigInteger começo = new BigInteger("1");
BigInteger fim = new BigInteger("100");
BigInteger n0 = new BigInteger("0");
BigInteger n2 = new BigInteger("2");
BigInteger n3 = new BigInteger("3");
for (BigInteger a = começo; a.compareTo(fim) <= 0; a = a.add(BigInteger.ONE)) {
if (a.divide(n2)==n0){}
else if (a.divide(n3)==n0){}
else {
lista.add(a);
}
}
System.out.println( lista );
}
}
Thanks Buddy, how do I do the same instead of being Biginteger in Bigdecimal?
– Rafael
@Rafael Almost that. The difference is that the
equals(Object)
may not be enough, since in theBigDecimal
, he considers that the values123.456
and123.4560
are different because the zeros on the left are considered. In this case you use thecompareTo(BigDecimal)
or thesignum()
.– Victor Stafusa