草庐IT

CompareTo

全部标签

java - 未记录的 String.compareTo(null) NPE?

以下小测试引发NPE:publicclassTest{publicstaticvoidmain(String[]args){Stringa="a";Stringb=null;System.out.println(a.compareTo(b));}}然而,compareTo()的Javadoc没有提到参数不能为null。这很奇怪,因为Javadocs通常会提到参数不能为null。这只是文档中的一个小故障,还是我缺少更根本的原因/扭曲? 最佳答案 您可以在查看Comparable时得到一些解释。界面:Notethatnullisnota

java:在 BigInteger 的情况下 for 循环如何工作

我想将用户输入作为Big-Integer并将其操作到For循环中BigIntegeri;for(BigIntegeri=0;i但它不会起作用任何人都可以帮助我。 最佳答案 您可以改用以下语法:BigIntegeri=BigInteger.valueOf(100000L);//longi=100000L;i.compareTo(BigInteger.ONE)>0//i>1i=i.subtract(BigInteger.ONE)//i=i-1所以这里有一个把它放在一起的例子:for(BigIntegerbi=BigInteger.val

java:在 BigInteger 的情况下 for 循环如何工作

我想将用户输入作为Big-Integer并将其操作到For循环中BigIntegeri;for(BigIntegeri=0;i但它不会起作用任何人都可以帮助我。 最佳答案 您可以改用以下语法:BigIntegeri=BigInteger.valueOf(100000L);//longi=100000L;i.compareTo(BigInteger.ONE)>0//i>1i=i.subtract(BigInteger.ONE)//i=i-1所以这里有一个把它放在一起的例子:for(BigIntegerbi=BigInteger.val

java - 有没有 compareTo() 和 equals() 不一致的标准 Java 类?

我正在寻找compareTo()可以返回0而equals()返回false的所有标准Java类:Comparablecomparable1=???;Comparablecomparable2=???;assertcomparable1.compareTo(comparable2)==0;assert!comparable1.equals(comparable2);我只知道一个:newBigDecimal("1.0")等于newBigDecimal("1")使用compareTo()但不等于使用equals()。还有其他的吗?我对所有这些类都感兴趣,但仅限于公共(public)JavaA

java - 有没有 compareTo() 和 equals() 不一致的标准 Java 类?

我正在寻找compareTo()可以返回0而equals()返回false的所有标准Java类:Comparablecomparable1=???;Comparablecomparable2=???;assertcomparable1.compareTo(comparable2)==0;assert!comparable1.equals(comparable2);我只知道一个:newBigDecimal("1.0")等于newBigDecimal("1")使用compareTo()但不等于使用equals()。还有其他的吗?我对所有这些类都感兴趣,但仅限于公共(public)JavaA

java - Java 中 Comparable.compareTo 的返回值是什么意思?

compareTo()中返回0、返回1和返回-1有什么区别?在Java中? 最佳答案 官方定义来自Comparable.compareTo(T)的引用文档:Comparesthisobjectwiththespecifiedobjectfororder.Returnsanegativeinteger,zero,orapositiveintegerasthisobjectislessthan,equalto,orgreaterthanthespecifiedobject.Theimplementormustensuresgn(x.co

java - Java 中 Comparable.compareTo 的返回值是什么意思?

compareTo()中返回0、返回1和返回-1有什么区别?在Java中? 最佳答案 官方定义来自Comparable.compareTo(T)的引用文档:Comparesthisobjectwiththespecifiedobjectfororder.Returnsanegativeinteger,zero,orapositiveintegerasthisobjectislessthan,equalto,orgreaterthanthespecifiedobject.Theimplementormustensuresgn(x.co

c# - 应该是 IEquatable<T >'s Equals() be implemented via IComparable<T>' s CompareTo()?

我一直在互联网上寻找答案,但我找到的是:编辑:添加了一些响应答案的项目对于IEquatable我应该重载Equals(),GetHashCode(),==和!=一起。我应该通过实现!=来减少冗余通过==.我应该结束这个类对于IComparable我应该重载Equals(),GetHashCode(),,>,和>=一起。实际上建议在这样做时实现IEquatable重载IComparable的非泛型版本CompareTo()==0应该是Equals()==true所以我一直在想这个:publicboolEquals(Tother){if((object)other==null){retur

sorting - 为什么 mapreduce 二次排序不在复合键的 compareTo() 上?

要执行二次排序,我们必须创建一个复合键,它扩展了WritableComparable接口(interface)并实现了compareTo()。在“Hadoop:权威指南”一书和几乎所有其他地方的博客中,排序都是基于扩展WritableComparator的单独类(书中的KeyComparator)的compare()方法进行的。compare()和compareTo()具有相同的逻辑。既然WritableComparator的compare()是用来对compositekey进行排序的,那么CompositeKey的compareTo()到底什么时候用到呢?

hadoop - 自定义 hadoop 键和值 : How to write CompareTo() Method

我需要从映射器发出一个二维double组作为键和值。StackOverflow中发布了问题,但没有得到解答。我在给定的数据集中做一些矩阵乘法,之后我需要发出A*Atrns的值,它将是一个矩阵作为键和Atrans*D这也将是一个矩阵作为值。那么如何从映射器发出这些矩阵。并且该值应该与key本身相对应。iekey----->A*Atrans--------->aftermultiplicationtheresultwillbea2Darraywhichisdeclaredasdouble(matrix)letssaytheresultbeMatrix"Ekey"(double[][]Eke