小编chr*_*ina的帖子

python将bytearray转换为列表中的数字

对于以下python代码:

pt  = bytearray.fromhex('32 43 f6 a8 88 5a 30 8d 31 31 98 a2 e0 37 07 34')
state = bytearray(pt)
Run Code Online (Sandbox Code Playgroud)

如果我使用:

print state
Run Code Online (Sandbox Code Playgroud)

它发出 2Cö¨ˆZ0?11˜¢à74

那么如何恢复中的内容bytearray呢?例如,将它们放在类似的列表中[]

python bytearray

3
推荐指数
2
解决办法
1万
查看次数

获得D3中的元素颜色

对于以下d3代码:

var previousElement = d3.select(this);
console.log(previousElement.attr("style"));
Run Code Online (Sandbox Code Playgroud)

结果如下:

fill: red;
Run Code Online (Sandbox Code Playgroud)

要么

fill: rgb(152, 223, 138);
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能得到这个元素的确切颜色,这样我就可以做出如下判断:

if ( elementColor == 'red')
Run Code Online (Sandbox Code Playgroud)

要么

if (elementColor == 'rgb(152, 223, 138)')
Run Code Online (Sandbox Code Playgroud)

我试过了var elementColor = previousElement.attr("style").fill,它回来了null.

javascript d3.js

3
推荐指数
1
解决办法
4597
查看次数

使用Stream实例化新对象

java8,我有一组字符串:

final Set<String> nameSet = this.getNames();
Run Code Online (Sandbox Code Playgroud)

我想得到一个列表People,People根据字符串设置名称Set.但是,People类没有类似的构造函数new People(name),它只能通过using setName方法实现.

以旧方式,我可以做类似的事情:

    List<People> peoples = new ArrayList<People>();
    for(String name: nameSet){
        People people = new People();
        people.setName(name);
        peoples.add(people);
    }
Run Code Online (Sandbox Code Playgroud)

我怎么能用它Stream来转换呢?

java java-8 java-stream

3
推荐指数
1
解决办法
566
查看次数

Java将字符串集合减少到出现的映射

考虑一个列表id1_f, id2_d, id3_f, id1_g,我如何使用流来获得<String, Integer>统计格式的简化地图,如:

id1 2
id2 1
id3 1
Run Code Online (Sandbox Code Playgroud)

注意:关键是之前的部分_.reduce功能在这方面有帮助吗?

java mapreduce java-stream

3
推荐指数
1
解决办法
99
查看次数

当密钥不存在时,Hashtable返回null或抛出异常?

当我创建对象的新实例作为键时,Java HashMap.get()返回null

对于上面的链接,似乎HashMap在地图中不存在键时返回null.但是,当我尝试:

Map<Integer,Integer> hs = new HashMap<Integer,Integer>();
int value = hs.get(1);
System.out.println(value);
Run Code Online (Sandbox Code Playgroud)

它抛出:

Exception in thread "main" java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)

有什么问题?如何让Map返回值为null而不是在这种情况下抛出异常?

java dictionary

2
推荐指数
1
解决办法
1046
查看次数

CQL选择特定列

对于以下Cassandra架构:

CREATE TABLE periods (
period_name text,
event_name text,
event_date timestamp,
weak_race text,
strong_race text,
PRIMARY KEY (period_name, event_name, event_date)
);
Run Code Online (Sandbox Code Playgroud)

通常select声明可以是:

SELECT * FROM ruling_stewards
WHERE king = 'Brego'
AND reign_start >= 2450
AND reign_start < 2500 ALLOW FILTERING;
Run Code Online (Sandbox Code Playgroud)

但有没有办法选择特定的列而不给出关系?例如,要显示所有event_nameperiod_name列?(不要显示其他未提及的列).

cql cassandra cqlsh

2
推荐指数
1
解决办法
1805
查看次数

Java,检测一个String是否超出Long类型的值边界

给出以下Java代码:

String columnValue = "188237574385834583453453635";
columnValue =(Long.parseLong(columnValue)>Long.MAX_VALUE?"0":columnValue);
Run Code Online (Sandbox Code Playgroud)

它会抛出,java.lang.NumberFormatException因为输入值超出了Long最大值.但是,有一种简单的方法可以检测"字符串"类型中的数字是否Long超出了使用try catch解决方案的最大值?

java long-integer

2
推荐指数
1
解决办法
257
查看次数

使用Hashmap查找单个整数

编码问题:Given an array of integers, every element appears twice except for one. Find that single one.它需要a linear runtime complexity.

我的解决方案

public class Solution {
    public int singleNumber(int[] A) {
    HashMap<Integer, Integer> lut = new HashMap<Integer, Integer>();
     for (int i=0; i<A.length; i++){
         if(lut.containsKey(A[i])){
             lut.remove(A[i]);
         }
         else{
             lut.put(A[i],1);
         }
     }

     Set<Integer> keys = lut.keySet();
     System.out.println(keys.size());
     for(Integer integer: keys)
         return integer;

     return (Integer) null;
 }
Run Code Online (Sandbox Code Playgroud)

}

我认为复杂性是O(n)因为它只通过数组一次并HashMap使用固定时间来获取元素.但在网上判断之后,就说出了我的代码time limit.有人可以指出为什么这超过了时间限制?如何提高?

java arrays hashmap

2
推荐指数
1
解决办法
395
查看次数

扩展类的constuctor被调用两次

对于以下代码,结果是

我在B,值是0
我在B,值是44
22

public class Test {
    public static void main(String[] args) {
        P b = new B();
        System.out.println(b.a);
    }

    static class P {
        public int a = 11;

        public P() {
            a = 22;
            diplay();
        }

        public void diplay() {
            System.out.println("I am in P, value is " + a);
        }
    }

    static class B extends P {
        int a = 33;

        public B() {
            a = 44;
            diplay();
        }

        public void diplay() {
            System.out.println("I am in B, value …
Run Code Online (Sandbox Code Playgroud)

java constructor

2
推荐指数
1
解决办法
91
查看次数

没有范围键的 DynamodbDB 查询

我有两列定义的表,第 1hash key列是 ,第 2 列是range key. 我想获得使用相同哈希键定义的所有项目(因此范围键无关紧要)。

我尝试使用new KeyPair().withHashKey(k). 但它会抛出异常说no RANGE key value present

我唯一的选择是对表格进行扫描以实现这一目标吗?

amazon-dynamodb dynamodb-queries

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