compareto Questions

18

Solved

I'm implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform): public class Metadata implements Comparab...
Mastectomy asked 26/1, 2009 at 23:25

5

Solved

I want to do something like this: class Foo extends Ordered[Foo] { val x val y val z . . . . def compare(that: Foo) = { val c0 = this.length compareTo that.length // primary comparison l...
Milka asked 4/2, 2013 at 21:18

6

Solved

It is said that when input parameter is null, compareTo() should throw a NullPointerException. However, I am implementing a class which needs to compare fields with the type of String. These fields...
Newlin asked 6/6, 2011 at 23:56

3

Solved

Working on a sorted list I came to a point I needed to implement a compareTo() function for primitive long values. I'm not looking for the obvious naive implementation, but was wondering if there'...
Transitory asked 28/6, 2015 at 14:30

5

Solved

I have created a Student class like this: public class Student implements Comparable<Student> { private String firstName; private String lastName; public Student(String firstName, Strin...
Chaunce asked 10/7, 2015 at 7:14

21

Solved

When testing for equality of String's in Java I have always used equals() because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. ...
Suez asked 11/10, 2009 at 17:35

2

Solved

I am planning to sort keys from a hashmap. I am using a customized sort method. Following code gives me compile-time error on compareTo() method where I am using Set as Collection Set<String&...
Untold asked 24/1, 2020 at 20:58

4

Solved

Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // T...
Fabric asked 22/7, 2011 at 7:56

16

Solved

What is the difference between compare() and compareTo() methods in Java? Do those methods give the same answer?
Keneth asked 7/1, 2009 at 12:59

3

Solved

Quoted from Effective Java - Second Edition by Joshua Bloch For floating-point fields, use Double.compare or Float.compare in place of the relational operators, which do not obey the general con...
Tie asked 12/10, 2012 at 20:51

5

Solved

I have an assignment where I need to create a Student class where you store the student's neptun code (String nep_c) and the number of points you have achieved in the exam (int point_num). Prepare ...
Courageous asked 3/1, 2019 at 12:57

9

Solved

I am learning about arrays, and basically I have an array that collects a last name, first name, and score. I need to write a compareTo method that will compare the last name and then the first na...
Nidifugous asked 4/4, 2012 at 18:46

5

Solved

In my program an array fClasses of fixed length [7] of objects is created, each object is a class FClass that contains 3 Strings, an int, and an int[]. These values are read from a .txt file and ad...
Ley asked 18/12, 2014 at 21:33

2

Solved

Is there some way to determine which of two BigDecimal objects is the lower (smaller) number that is simpler than an if or a ternary operator calling BigDecimal::compareTo? Given: BigDecimal x = ...
Giralda asked 21/5, 2018 at 5:43

6

Solved

I have been looking for an answer for some time now, but nowhere could I actually find it. I was especially looking at this page. There it says that the CompareTo method returns an integer indicat...
Backandforth asked 2/12, 2012 at 11:48

8

Solved

Is it better to write int primitive1 = 3, primitive2 = 4; Integer a = new Integer(primitive1); Integer b = new Integer(primitive2); int compare = a.compareTo(b); or int primitive1 = 3, primiti...
Veneration asked 5/2, 2012 at 15:30

3

Solved

I have some numbers I'm trying to compare. They represent lengths of paths through different spaces. Unfortunately for me, some imprecision was causing false comparisons. For instance, after noti...
Branscum asked 30/10, 2017 at 15:18

6

Solved

I have a class that implements the Comparable interface. In this class I need to override compareTo method in order to sort objects by Long values. What I don't know is how to perform is the compa...
Chanell asked 10/10, 2013 at 16:42

4

I know this has been an issue for a while now, and checked all previously answers I could get, but still this one doesn't work. The object 'crew' represents crewmembers with ranks and other items....
Byssus asked 21/10, 2011 at 12:42

4

Solved

The line return array[index1].compareTo(array[index2]); provides an error "Cannot invoke compareTo(double) on the primitive type double". How to solve this issue? /*:::::::::::::::::::::::::::::::...
Repeater asked 22/11, 2013 at 10:9

1

Solved

I've got the following class: //GetHasCode, toString, and equalsTo removed to keep the question simple. private String weaponName; private String weaponType; private int weaponDamage; public Weap...
Yetac asked 16/3, 2016 at 19:41

2

Solved

So I need to do dynamic ordered list. public class DynArrayListOrd<T extends Comparable<T>> { private T[] tab ; public DynArrayListOrd() { tab = (T[])new Object[startSize]; } ....
Cloak asked 16/1, 2016 at 13:39

5

Solved

An enum in Java implements the Comparable interface. It would have been nice to override Comparable's compareTo method, but here it's marked as final. The default natural order on Enum's compareTo ...
Vassaux asked 6/2, 2009 at 10:22

3

I know that compareTo returns a negative or positive result on how well one string correlates to the other, but then why: public class Test { public static void main(String[] args) { String y = ...
Adler asked 7/9, 2015 at 17:13

1

Solved

Suppose I want to sort a list of Employee objects: Employee emp1 = new Employee("Abhijit", 10); Employee emp2 = new Employee("Aniket", 5); Employee emp3 = new Employee("Chirag", 15); List<Empl...
Twaddle asked 23/7, 2015 at 12:25

© 2022 - 2024 — McMap. All rights reserved.