我想有一个compareTo方法,它接受一个Real(一个用于处理任意大而精确的实数的类[好吧,只要它的长度小于2 ^ 31])和compareTo方法一个对象,但Java不让我,我没有足够的经验知道为什么.
我只是尝试修改类来实现Comparable,我在下面收到了这些错误消息.我真的不明白错误信息是什么意思,但我知道它与可怕的方式有关,我试图给我们的课程一些灵活性,为我制作的每一种方法提供所有不同的方法签名,我可以修复它通过删除compareTo(Object other)方法,但我最好保留它.所以我真正要问的是:有没有办法让这些错误消息消失而不删除compareTo(Object other)方法,这些错误究竟是什么意思?
另外,我知道已经有一些像BigInteger这样的内置Java类,以及我正在尝试使用这个类的东西,但我正在为了与Project Euler(https://一起使用)的乐趣/满足而这样做projecteuler.net/).
Jake@Jake-PC /cygdrive/c/Users/Jake/Documents/Java/Mathematics
$ javac Real.java
Real.java:377: error: name clash: compareTo(Real) in Real overrides a method whose erasure is the same as another method, yet neither overrides the other
public int compareTo(Real other)
^
first method: compareTo(Object) in Real
second method: compareTo(T) in Comparable
where T is a type-variable:
T extends Object declared in interface Comparable
Real.java:440: error: name clash: compareTo(Object) in Real and compareTo(T) in Comparable have the …
Run Code Online (Sandbox Code Playgroud) 我想删除没有该表单的文件中的所有行:
something.something,东西,东西
例如,如果文件如下:
A sentence, some words
ABCD.CP3,GHD,HDID
Hello. How are you?
A.B,C,D
dbibb.yes,whoami,words
Run Code Online (Sandbox Code Playgroud)
我会留下:
ABCD.CP3,GHD,HDID
A.B,C,D
dbibb.yes,whoami,words
Run Code Online (Sandbox Code Playgroud)
我试图分支到sed脚本的末尾,如果我匹配我不想删除的模式但是如果它不匹配则继续并删除该行:
cp $file{,.tmp}
sed "/^.+\..+,.+,.+$/b; /.+/d" "$file.tmp" > $file
rm "$file.tmp"
Run Code Online (Sandbox Code Playgroud)
但这似乎没有任何影响.
我想我可以逐行读取文件,检查是否匹配模式,如果有,则将其输出到文件,但我想使用sed或类似的方法.
我刚刚开始学习 Scala,为了练习,我决定创建一个 Pair[A, B] 类,该类在排序时首先按 As 排序,然后按 Bs 排序。我的第一次尝试是这样的:
case class Pair[A <: Ordered[A], B <: Ordered[B]](val left: A, val right: B) extends Ordered[Pair[A, B]]
{
override def compare(that: Pair[A, B]) = {
val leftCompare = this.left.compare(that.left)
if (leftCompare == 0)
this.right.compare(that.right)
else
leftCompare
}
}
object Main extends App
{
List(Pair(1, "a"), Pair(5, "b"), Pair(5, "a"), Pair(1, "b")).sorted
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要的方式,真的。我想扩展 Ordered 并让它工作,所以我可以做像 List(whatever).sortWith(_ < _) 或 List(whatever).sorted 之类的事情,就像我在 Main 中写的那样。我收到以下错误:
pair.scala:14: error: inferred type arguments [Int,String] do not conform …
Run Code Online (Sandbox Code Playgroud) 我有以下谓词,我写的是为了识别两个列表是相同的,除了索引处的两个元素I1
并且I2
被交换:
swapped(I1, I2, List, NewList) :-
% The lists are the same length and the two indices are swapped.
same_length(List, NewList),
nth0(I1, List, V1), nth0(I2, List, V2),
nth0(I1, NewList, V2), nth0(I2, NewList, V1),
% All the other indices remain the same.
proper_length(List, Length), Lim is Length - 1,
numlist(0, Lim, Indices),
forall((member(I, Indices), I \= I1, I \= I2),
(nth0(I, List, V), nth0(I, NewList, V))).
Run Code Online (Sandbox Code Playgroud)
以下swipl
输出演示了我的问题:
?- swapped(0, 1, [1,2,3], L).
L = [2, …
Run Code Online (Sandbox Code Playgroud) 我正在做一些需要使用GCD算法的事情,我希望它尽可能快。我尝试了普通方法,二进制方法和备忘录方法,我认为它们会比以前更好。我从这里复制了二进制方法,进行了一些细微调整。
我一直在使用一个名为TestGCD的类进行测试,这就是整个过程:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestGCD
{
private static class Pair<A>
{
private final A a_one;
private final A a_two;
public Pair(A a_one, A a_two)
{
this.a_one = a_one;
this.a_two = a_two;
}
@Override
public boolean equals(Object object)
{
if (this == object)
return true;
if (object == null)
return false;
if (!(object instanceof Pair))
return false;
final Pair other = (Pair) object;
if (a_one == null)
if (other.a_one != null) …
Run Code Online (Sandbox Code Playgroud)