对于一个scanner对象,该hasNextLine()
方法返回false时该hasNext()
方法返回true是怎么回事?
注意:根据输入文件,该hasNext()
方法将按预期返回结果; 在hasNextLine()
似乎没有被返回正确的结果.
这是我正在运行的代码,它创建了以下结果:
public void ScannerTest(Reader fileReaderObject){
Scanner scannerObj = new Scanner(fileReaderObject);
for(int i = 1; scannerObj.hasNext(); i++){
System.out.println(i + ": " + scannerObj.next());
System.out.println("Has next line: " + scannerObj.hasNextLine());
System.out.println("Has next: " + scannerObj.hasNext());
}
System.out.println();
scannerObj.close();
}
Run Code Online (Sandbox Code Playgroud)
以下是我传递给此扫描器的文件的实际内容:
a 3 9
b 3 6
c 3 3
d 2 8
e 2 5
f 2 2
g 1 7
h 1 4
i 1 1
Run Code Online (Sandbox Code Playgroud)
现在我想查找是否有包含'\'字符的行.我试过grep "\\" XXFile
但它暗示"Trailing Backslash".但是,当我尝试grep '\\' XXFile
它是可以的.谁能解释为什么第一个案例无法运行?谢谢.
我在我的~/Sites
目录中有一个文件,当我浏览它时它工作正常coderama.local/~coderama/index2.php
现在我想变得棘手并将我的index2.php
文件移动到我系统上的其他位置,所以我通过创建符号链接来实现这一点.但是,当我尝试访问时,coderama.local/~coderama/index2.php
我现在得到以下错误.
任何人的想法?
谢谢!
被禁止
您无权访问此服务器上的/~coderama/index2.php.
我在算法设计中有一个关于复杂性的问题.在这个问题中给出了一段代码,我应该计算这段代码的复杂性.伪代码是:
for(i=1;i<=n;i++){
j=i
do{
k=j;
j = j / 2;
}while(k is even);
}
Run Code Online (Sandbox Code Playgroud)
我为一些数字尝试了这个算法.我得到了不同的结果.例如,如果n = 6,则该算法输出如下所示
i = 1 -> executes 1 time
i = 2 -> executes 2 times
i = 3 -> executes 1 time
i = 4 -> executes 3 times
i = 5 -> executes 1 time
i = 6 -> executes 2 times
Run Code Online (Sandbox Code Playgroud)
它没有常规主题,我该如何计算呢?
这是一种我无法理解的怪异行为.在我的例子我有一个类Sample<T>
和隐式转换操作符T
来Sample<T>
.
private class Sample<T>
{
public readonly T Value;
public Sample(T value)
{
Value = value;
}
public static implicit operator Sample<T>(T value) => new Sample<T>(value);
}
Run Code Online (Sandbox Code Playgroud)
使用空值类型为出现问题时,T
例如int?
.
{
int? a = 3;
Sample<int> sampleA = a;
}
Run Code Online (Sandbox Code Playgroud)
这里是关键部分:
在我看来,这不应该编译,因为Sample<int>
定义了一个转换,从int
以Sample<int>
而不是从int?
到Sample<int>
.但它编译并成功运行!(我的意思是调用转换运算符3
并将其分配给该readonly
字段.)
它变得更糟.这里不调用转换运算符,sampleB
将其设置为null
:
{
int? b = null;
Sample<int> sampleB = b; …
Run Code Online (Sandbox Code Playgroud) 我正在阅读有关Java 和的区别的问题,这已经有几年了。令我惊讶的是,只有一个问题提到了使用的任何缺点; 也就是说,如果您使用大量CPU,则速度会降低。Arrays.sort
Arrays.parallelSort
parallelSort
假设您不在某种专门的单线程环境中,应该总是选择一个parallelSort
吗?有没有理由不这样做?请注意,上述问题的答案之一是,如果少于4096个元素,则无论如何都会parallelSort
调用sort
。
我创建了一个sqlite数据库,它有一个存储温度值的表.温度值首次以升序写入数据库.然后我将数据库中的温度值读入列表,然后将该列表添加到组合框中以选择温度 - 工作正常.
结果列表是:
templist = ['25', '50', '100', '150', '200', '250', '300'].
Run Code Online (Sandbox Code Playgroud)
然后我向数据库添加一个新的温度值,比如'33'.
它被附加到表的末尾.如果我现在读取温度,列表将变为:
['25', '50', '100', '150', '200', '250', '300', '33'].
Run Code Online (Sandbox Code Playgroud)
如果我做templist.sort()
或sorted(templist)
,最终的结果是
['150', '200', '25', '250', '300', '33', '50']
Run Code Online (Sandbox Code Playgroud)
是否有任何简单的方法按升序对列表进行排序,以便我得到:
['25', '33', '50', '100', '150', '200', '250', '300']
Run Code Online (Sandbox Code Playgroud) 当比较两个指针变体 - 经典与shared_ptr时 - 我对程序运行速度的显着提高感到惊讶.为了测试2D Delaunay增量插入算法已被使用.
编译器设置:
VS 2010(发布)/ O2/MD/GL,W7 Prof,CPU 3.GHZ DualCore
结果:
shared_ptr(C++ 0x00):
N[points] t[sec]
100 000 6
200 000 11
300 000 16
900 000 36
Run Code Online (Sandbox Code Playgroud)
指针:
N[points] t[sec]
100 000 0,5
200 000 1
300 000 2
900 000 4
Run Code Online (Sandbox Code Playgroud)
shared_ptr版本的运行时间大约是其10倍.这是由编译器设置引起的还是C++ 0x00 shared_ptr实现那么慢?
VS2010 Profiler:对于原始指针,大约60%的时间花费在启发式搜索包含插入点的三角形上(这是一个众所周知的事实).但是对于shared_ptr版本,大约58%的时间花在使用shared_ptr.reset()上,只有10%用于启发式搜索.
void DT2D::DT ( Node2DList *nl, HalfEdgesList *half_edges_dt, bool print )
{
// Create 2D Delaunay triangulation using incremental insertion method
unsigned int nodes_count_before = nl->size();
// Remove duplicit points …
Run Code Online (Sandbox Code Playgroud) 我花了一些时间开始研究关于流和lambdas的java-8嗡嗡声.让我吃惊的是,你不能应用流操作,例如.map()
,.filter()
直接上java.util.Collection
.是否存在技术原因导致java.util.Collection
接口未通过这些Stream操作的默认实现进行扩展?
谷歌搜索了一下,我看到很多人按照以下模式编码的例子:
List<String> list = someListExpression;
List<String> anotherList = list.stream().map(x -> f(x)).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果你的代码中有很多这些流操作,那就变得非常笨拙了.由于.stream()
并且.collect()
与您想表达的内容完全无关,您宁愿说:
List<String> list = someListExpression;
List<String> anotherList = list.map(x -> f(x));
Run Code Online (Sandbox Code Playgroud) 我很惊讶这个断言失败了:
x = 42
x = lambda: x
assert x() == 42
Run Code Online (Sandbox Code Playgroud)
似乎x
结束了递归指本身,这样x()
,x()()
等都是功能。
用于解析它的规则是什么,它记录在哪里?
顺便说一句(不出乎意料地给出了上面的内容), 的原始值x
在 lambda 定义之后没有引用:
class X:
def __del__(self): print('deleting')
x = X()
x = lambda: x # 'deleting' is printed here
Run Code Online (Sandbox Code Playgroud)