相关疑难解决方法(0)

Java Integer compareTo() - 为什么使用比较与减法?

我发现方法的java.lang.Integer实现compareTo如下:

public int compareTo(Integer anotherInteger) {
    int thisVal = this.value;
    int anotherVal = anotherInteger.value;
    return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
Run Code Online (Sandbox Code Playgroud)

问题是为什么使用比较而不是减法:

return thisVal - anotherVal;
Run Code Online (Sandbox Code Playgroud)

java optimization comparison integer overflow

78
推荐指数
3
解决办法
1万
查看次数

如何对Collection <T>进行排序?

我有一个通用的Collection,我试图找出如何排序其中包含的项目.我尝试了一些东西,但我不能让它们中的任何一个工作.

java sorting collections

51
推荐指数
5
解决办法
11万
查看次数

使用Collections.sort()方法按字母顺序对对象进行排序

我试过用Collections.sort(shapes)它但它给了我这个错误:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the 
 arguments (ArrayList<CreateShape>). The inferred type CreateShape is not a valid substitute for the 
 bounded parameter <T extends Comparable<? super T>>
Run Code Online (Sandbox Code Playgroud)

我该怎么解决这个问题?

CreateSpace类

 public class CreateSpace implements Space{

            private int height;
        private int width;
        private String layout;
        private char[][] space;
        private Shape originalShape;
        private ArrayList<CreateShape> shapes = new ArrayList<CreateShape>();

        public CreateSpace(int height, int width, char[][] space, String layout)
        {
            this.height = height;
            this.width = …
Run Code Online (Sandbox Code Playgroud)

java

5
推荐指数
1
解决办法
1046
查看次数

ArrayList的排序

我有一个数组列表

ArrayList itemListWithRank = ItemListDAO.getItemList();
Run Code Online (Sandbox Code Playgroud)

在arraylist中itemListWithRank有许多类型的对象值,它们都是不同的.并且它们中的一个值是也使用该数组列表设置的项目等级.

现在我想根据排名的加入顺序对这个数组列表进行排序.已在此数组列表中设置排名值.

我如何对arraylist进行排序哪一个有很多类型的值....?

谢谢大家....

java sorting arraylist

2
推荐指数
1
解决办法
6773
查看次数

用Java对对象数组进行排序

我正在编写一个报告模块,显示花费的时间和任务数量.这些值在Java Bean中设置,bean对象存储在一个数组中.我使用单独的查询来获取任务的时间和数量.现在我必须根据任务的时间和数量对数组进行排序.下面的代码仅比较字符串:

if (!list.isEmpty()) {
    Collections.sort(list, new Comparator<Project>() {
        @Override
        public int compare(Project p1, Project p2) {

            return p1.getName().compare(p2.getName());
        }
       });
   }
Run Code Online (Sandbox Code Playgroud)

我在排序存储在数组中的JavaBean中的属性的整数值时遇到问题.非常感谢任何帮助.

java sorting

2
推荐指数
1
解决办法
8335
查看次数

按数值排序列表<>

我有一个public List<FriendProfile> friends = new ArrayList<FriendProfile>();.我通过从服务器读取信息来初始化好友列表.FriendProfile对象包含一个名为的intprivate int userPosition;

一旦朋友列表被初始化,我想通过让FriendProfile对象具有userPosition列表中索引0 的最高位置对朋友列表进行排序,然后相应地排序,索引1具有第二高userPosition...

我想我可以写一个排序算法,但我正在寻找预先编写的代码(也许JDK有一些提供的方法?)

感谢帮助!

java sorting android list

1
推荐指数
1
解决办法
1万
查看次数