小编Fre*_*ind的帖子

如何使用css选择器查​​找不在一个div内的所有输入?

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.这该怎么做?

html css css-selectors

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

向其中写入一些数据后无法删除该文件,哪里出了问题?

我在文件中写入一些文本,然后将其删除,但是删除失败。

代码很简单:

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)

java file-io file delete-file

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

robolectric,没有意图匹配`receiveResult`

看我的代码:

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}是,它与后者完全相同.

错在哪里以及如何解决?

unit-testing android-intent robolectric

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

为什么java内置序列化比Gson差?

我认为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)

java performance serialization gson

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

为什么我们不能两次混合特质?

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)

我想知道为什么我的代码看起来合理时有这样的限制?

inheritance scala traits

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

如何在某个sbt任务中使用另一个键的值?

说我已经定义了这样一个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)

我怎样才能获得的价值100mykey

scala sbt

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

使用`Future`运行一些永不完成的循环任务是否正确?

在我们的项目中,我们需要执行一个侦听队列并在循环中处理即将发生的消息的任务,该循环永远不会完成.代码看起来像:

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中最好的解决方案是什么.

scala future

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

如何声明一个可能返回3种不同类型的方法?

我想定义一个方法,但它的返回类型有3种.

def findSelectedItem: ??? = { ... }
Run Code Online (Sandbox Code Playgroud)

???这里可能是Category,Section,Page,但我不知道如何找到合适的类型来代表它.

如果它只是2,我可以使用Either[Type1, Type2],但它现在是3.

我是否需要声明类似Either但有3种类型变量的东西?或者我已经可以使用了什么?

types scala

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

如果它从外部读取一些数据而不是参数,它是纯函数吗?

在"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)

functional-programming scala

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

如何捕捉演员内部抛出的异常?

在我的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)

有没有办法在一个地方处理所有异常?我想一起处理它们,例如记录它们

scala actor akka

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

Android布局:一个在左边,一个在右边,一个在中心?

我需要这样的布局:

在此输入图像描述

button1位于左侧,button3位于右侧,button2位于中央.并且所有这些都在垂直方向的中间.

怎么实现呢?

layout android center

-1
推荐指数
1
解决办法
77
查看次数