标签: comparable

通过可比接口对ArrayList <Object>进行排序

实际上我想对Array List of objects进行排序.我为此目的使用Comparable接口.它完全正常工作但问题是,当我进行排序时,就是给出了这两个问题.

  1. 所有拥有首字母大写字母的名字都汇集在一起​​,所有名字第一个字母小的名字都在底部聚集在一起.

  2. 所有排序后的大写字母单词都汇集在一起​​,之后所有的小写字母一起出现在底部.

这是我的豆子

MyShares.java

   public class Myshares implements Comparable<Myshares> {

        int id, parent;
        String name, path, type, shared_with, shared_or_not, upload_request;

        public int getParent() {
            return parent;
        }

        public void setParent(int parent) {
            this.parent = parent;
        }

        public String getUpload_request() {
            return upload_request;
        }

        public void setUpload_request(String upload_request) {
            this.upload_request = upload_request;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public …
Run Code Online (Sandbox Code Playgroud)

java sorting arraylist comparable

0
推荐指数
2
解决办法
1468
查看次数

Java Generics和exetends Comparable

我需要构建一个Course具有通用字段的类(我不能添加另一个包含的字段Comparator).

我需要创建一个ComparatorCourse,以便可以使用它自己的CompareTo方法比较泛型字段(如果String-compareTo是字符串,如果是整数等等):

mothod getGeneric()返回通用字段.

new Comparator<Course<?>>( {
    public int compare(Course<?> o1, Course<? extends Comparable<?>> o2) {
        return (o1.getGeneric()).compareTo(o2.getGeneric());
    }
});
Run Code Online (Sandbox Code Playgroud)

java generics compareto comparable comparator

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

Java可比合同

我必须始终遵守为之建立的合同Comparable Interface吗?当合同的所有三个案例都不清楚时,是否有可接受的杠杆?

合同简单地说明:

If this object precedes that; return negative value
If this object equals that; return zero
If this object proceeds that; return positive value 
Run Code Online (Sandbox Code Playgroud)

我希望比较两个作为Point2D对象的点.使用该distance(Point2D point)行为比较两个点使得compareTo()出现二进制; 两个点的距离为零或距离不为零.

如果我要编写一个compareTo()覆盖默认值的行为,可能会如下:

private Point2D location;

public int compareTo(Object o)
{
    if(this.getLocation().distance(o.getLocation() == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

也许比较两个Point2D对象更适合使用简单的行为测试距离而不是麻烦实现Comparable.甚至可能有办法遵守我不知道的合同.

java contract comparable

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

Java Generic Misbound

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;

public interface Comparable<T> {

int compareTo(T other);

}

public class Joueur implements Comparable<Joueur> {

private int points;
private int idJoueur;

public Joueur(int aIdJoueur, int aPoints)
{
    points= aPoints;
    idJoueur = aIdJoueur;
}

public Joueur(int aIdJoueur, int aPoints)
{
    points= aPoints;
    idJoueur = aIdJoueur;
}

public int getIdJoueur()
{
    return idJoueur;
}

public int compareTo(Joueur autre) {
    // TODO Auto-generated method stub
    if (points < autre.points) return -1;
    if (points > autre.points) return 1;
    return …
Run Code Online (Sandbox Code Playgroud)

java generics comparable

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

排序功能提供不正确的结果

我想priorities[]按降序排序,即...2,1,0.当我执行下面提供的代码时,我收到未排序的数组,例如

18, 14, 15, 19, 23, 37, 35, 1, 8, 24, 26, 36
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

double[] priorities = new double[10];
for (int i = 0; i < 10; i++) 
    priorities[i] = Math.round(10*Math.random();
ArrayIndexComparator comparator = new ArrayIndexComparator(priorities,1);
Integer[] sortedPriorities = comparator.createIndexArray();
Arrays.sort(sortedPriorities, comparator);


public class ArrayIndexComparator implements Comparator<Integer>
{
    private final double[] array;
    private int sort;

    public ArrayIndexComparator(double[] array, int sort)
    {
        this.array = array;
        this.sort = sort;
    }

    public Integer[] createIndexArray()
    {
        Integer[] indexes = new …
Run Code Online (Sandbox Code Playgroud)

java sorting comparable comparator

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

使用Generic Comparable <T>数据在Java中实现二叉树?

问:在我下面的二叉树实现中,为什么编译器会窒息

if (data.compareTo(this.data) <= 0),

生产

Error: incompatible types: java.lang.Comparable<T> cannot be converted to T

这两个datathis.data的类型Comparable<T>,应该能够使用或者是一个参数传递给的compareTo()方法,对吧?好吧,显然不是.但我真的不明白为什么.仿制药仍然令我感到困惑.

public class MyBinaryTreeNodeG<T>{
  Comparable<T> data;
  MyBinaryTreeNodeG<T> parent;
  MyBinaryTreeNodeG<T> left;
  MyBinaryTreeNodeG<T> right;

  public MyBinaryTreeNodeG(Comparable<T> data){
   this.data = data; 
  }

  public MyBinaryTreeNodeG<T> addChild(Comparable<T> data){
    if (data.compareTo(this.data) <= 0) { //this is the line on which the compiler chockes
    //check if left tree node is null. If so, add. Otherwise, recurse.
    } else {
    //same for the right …
Run Code Online (Sandbox Code Playgroud)

java generics binary-tree comparable

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

绑定不匹配:通用类(扩展Comparable的Generic类(扩展Comparable的Generic类))

我知道这听起来很混乱,但这是我能解释的最好的.(你可以建议一个更好的标题).我有3个班: -

A

public class A <T extends Comparable<T>> {
    ...
}
Run Code Online (Sandbox Code Playgroud)

B

public class B {
    A<C> var = new A<C>(); 
    // Bound mismatch: The type C is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type A<T>
    ...
}
Run Code Online (Sandbox Code Playgroud)

C

public class C <T extends Comparable<T>> implements Comparable<C>{
    private T t = null;
    public C (T t){
        this.t = t; 
    }
    @Override
    public int compareTo(C o) {
        return t.compareTo((T) o.t);
    }
    ...
} …
Run Code Online (Sandbox Code Playgroud)

java generics comparable

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

如何查看对象是否更接近相等

我有方法:

<T extends Comparable<T>> T moreApproxEqual(T object, T less, T greater) {
    //TODO: return less or greater, depending on which is closer to object
    return null;
}
Run Code Online (Sandbox Code Playgroud)

我有一个T列表,从中我发现变量"less"和"greater"是列表中最接近变量"object"的两个值.有什么方法我可以判断两个对象中的一个是否更接近"对象"而没有关于对象的更多信息?

java generics comparable approximate

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

如果参数是同一个接口的不同实现,compareTo()返回什么?

有接口

interface Animal extends Comparable<Animal> {
}
Run Code Online (Sandbox Code Playgroud)

和2个班级

class Dog implements Animal {

}
Run Code Online (Sandbox Code Playgroud)

class Cat implements Animal {

}
Run Code Online (Sandbox Code Playgroud)

compareTo(Animal o)当争论不是同一个具体实施时应该返回什么Animal

应该扔IllegalArgumentException吗?

例如,如果我将Dog实例传递给Cat.compareTo().我无法比较它们,因为它们是不同的类型.我不能称super.compareTo()他们的超级是Object没有实现的类型Comparable.铸造DogCat会抛出ClassCastException.

java interface comparable

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

了解Java中compareTo()方法的语法

我创建了一个Student类,其中包含卷号,名称和年龄作为其数据变量。我创建了一个ArrayList用于存储class的多个对象的Student

现在,我已使用该Comparable接口及其compareTo方法根据年龄对学生列表数据进行排序。

以下是compareTo我创建的按年龄排序的方法:

    public int compareTo(Student sNew)
    {
        return this.age-sNew.age;
    }
Run Code Online (Sandbox Code Playgroud)

在这里,我不明白,什么是-?以及它如何运作?

因为,我也这样做如下:

public int compareTo(Student sNew)
    {
        int returnValue=0;
        if(this.age>sNew.age)
        {
            returnValue=1;
        }else if(this.age<sNew.age){
            returnValue=-1;
        }else if(this.age==sNew.age)
        {
            returnValue=0;
        }
        return returnValue;
    }
Run Code Online (Sandbox Code Playgroud)

因此,我有两个怀疑:compareTo()方法中的“-”如何工作以及它在哪里返回值(0,1,-1)。

请指导。

java object compareto comparable

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