Html代码:
<div id="div1">
<div class="nest">
<input name="i1"/>
</div>
<div>
<input name="i2"/>
</div>
<input name="i3"/>
<div>
<input name="i4" />
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
现在我想找到所有输入内部#div1但不在内部.nest的css选择器,它们是i2,i3,i4.这该怎么做?
我在文件中写入一些文本,然后将其删除,但是删除失败。
代码很简单:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TestFile {
public static void main(String[] args) throws IOException {
File file = new File("c:\\abc.txt");
writeFile(file, "hello");
// delete the file
boolean deleted = file.delete();
System.out.println("Deleted? " + deleted);
}
public static void writeFile(File file, String content) throws IOException {
OutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(content.getBytes("UTF-8"));
} catch (IOException e) {
try {
out.close();
} catch (IOException e1) {
// ignored
}
} …Run Code Online (Sandbox Code Playgroud) 看我的代码:
doSomeActionWillStartNewIntentToSelectImage();
// check started intent
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
Assert.assertThat(activity, new StartedMatcher(intent));
// simulate the returning result
shadowOf(activity).receiveResult(
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI),
Activity.RESULT_OK,
new Intent().setData(activity.drawableId2Uri(R.drawable.icon_ok)));
Run Code Online (Sandbox Code Playgroud)
但它报告错误:
java.lang.RuntimeException: No intent matches
Intent{action=android.intent.action.PICK, data=content://media/internal/images/media} among
[Intent{action=android.intent.action.PICK, data=content://media/internal/images/media}]
at com.xtremelabs.robolectric.shadows.ShadowActivity.receiveResult(ShadowActivity.java:381)
Run Code Online (Sandbox Code Playgroud)
奇怪的Intent{action=android.intent.action.PICK, data=content://media/internal/images/media}是,它与后者完全相同.
错在哪里以及如何解决?
我认为Java内置序列化的性能应该很好.与Gson相比,它不需要进行词法分析,它应该比Gson更快.
但在我的测试中,结果恰恰相反.看我的代码:
package cleancode;
import com.google.gson.Gson;
import java.io.*;
public class SerializationPerformanceTest {
public static final int MAX_LOOP = 1000000;
public static void main(String[] args) throws Exception {
trySerialization();
tryGson();
}
private static void tryGson() {
long start = System.currentTimeMillis();
Gson gson = new Gson();
for (int i = 0; i < MAX_LOOP; i++) {
String json = gson.toJson(createUser());
gson.fromJson(json, User.class);
}
long end = System.currentTimeMillis();
System.out.println("Gson cost: " + (end - start) + "ms");
}
private static void trySerialization() throws …Run Code Online (Sandbox Code Playgroud) Scala代码:
trait Box {
def put(num:Int) = println("Put number: " + num)
}
trait DoubleBox extends Box {
override def put(num:Int) = super.put(2*num)
}
object MyBox extends Box with DoubleBox
MyBox.put(3)
Run Code Online (Sandbox Code Playgroud)
它工作正常并打印 6
但是当我尝试:
object MyBox extends Box with DoubleBox with DoubleBox
Run Code Online (Sandbox Code Playgroud)
它无法编译:
error: trait DoubleBox is inherited twice
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我的代码看起来合理时有这样的限制?
说我已经定义了这样一个sbt任务:
name := "hello"
version := "1.0"
scalaVersion := "2.11.0"
val mykey = settingKey[Int]("demo key")
mykey := 100
val hello = taskKey[Unit]("demo task")
hello := {println("Hello, world" + mykey)}
Run Code Online (Sandbox Code Playgroud)
我已经定义了一个mykey值为100,我想在另一个自定义任务中使用它hello,但是当我运行时:
$ sbt
> hello
Run Code Online (Sandbox Code Playgroud)
它输出:
Hello, worldsbt.SettingKey$$anon$4@66db9b08
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得的价值100的mykey?
在我们的项目中,我们需要执行一个侦听队列并在循环中处理即将发生的消息的任务,该循环永远不会完成.代码看起来像:
def processQueue = {
while(true) {
val message = queue.next();
processMessage(message) match {
case Success(_) => ...
case _ => ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我们想在一个单独的线程中运行它.
我可以想象两种方法,一种是用Thread我们在Java中做的:
new Thread(new Runnable() { processQueue() }).start();
Run Code Online (Sandbox Code Playgroud)
另一种方式是使用Future(我们现在这样做):
Future { processQueue }
Run Code Online (Sandbox Code Playgroud)
我只是想知道Future在这种情况下使用是否正确,因为我知道(这可能是错误的),Future意味着要运行一些任务,将在未来的某个时间完成或返回结果.但我们的任务永远不会完成.
我也想知道scala中最好的解决方案是什么.
我想定义一个方法,但它的返回类型有3种.
def findSelectedItem: ??? = { ... }
Run Code Online (Sandbox Code Playgroud)
在???这里可能是Category,Section,Page,但我不知道如何找到合适的类型来代表它.
如果它只是2,我可以使用Either[Type1, Type2],但它现在是3.
我是否需要声明类似Either但有3种类型变量的东西?或者我已经可以使用了什么?
在"Scala中的函数式编程"一书中,它提供了一些关于副作用的例子,例如:
我的问题是,从外部读取一些数据比参数使函数不纯?
例如
val name = "Scala"
def upcase() = name.toUpperCase
Run Code Online (Sandbox Code Playgroud)
upcase功能是否纯净?
编辑:根据这个答案:https://stackoverflow.com/a/31377452/342235,我的"函数"实际上不是函数,它是一个方法,所以我给它一个函数版本,并问同样的问题:
val name = "Scala"
val upcase: () => String = () => name.toUpperCase
Run Code Online (Sandbox Code Playgroud) 在我的actor中,方法可能会抛出异常:
class ServerActor extends Actor {
override def receive: Receive = {
case "Start" =>
println("### actor started")
throw new Exception("My exception when starting")
case msg =>
println("### actor get other message: " + msg)
throw new Exception("another exception for other messages: " + msg)
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在一个地方处理所有异常?我想一起处理它们,例如记录它们
我需要这样的布局:

button1位于左侧,button3位于右侧,button2位于中央.并且所有这些都在垂直方向的中间.
怎么实现呢?
scala ×6
java ×2
actor ×1
akka ×1
android ×1
center ×1
css ×1
delete-file ×1
file ×1
file-io ×1
future ×1
gson ×1
html ×1
inheritance ×1
layout ×1
performance ×1
robolectric ×1
sbt ×1
traits ×1
types ×1
unit-testing ×1