我有一个这样的程序:
class Test {
final int x;
{
printX();
}
Test() {
System.out.println("const called");
}
void printX() {
System.out.println("Here x is " + x);
}
public static void main(String[] args) {
Test t = new Test();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试执行它,我收到编译器错误:variable x might not have been initialized基于java默认值我应该得到以下输出权?
"Here x is 0".
Run Code Online (Sandbox Code Playgroud)
最终变量是否具有dafault值?
如果我改变我的代码,
class Test {
final int x;
{
printX();
x = 7;
printX();
}
Test() {
System.out.println("const called");
}
void printX() {
System.out.println("Here x is " + x); …Run Code Online (Sandbox Code Playgroud) 以下内容来自求职面试:
在包含整数的给定数组中,除了一个数字之外,每个数字重复一次,不重复.编写一个函数,查找不重复的数字.
我想过使用HashSet,但它可能会使一切变得复杂......
任何简单解决方案的想法?
我想将键值对作为查询参数附加到现有URL.虽然我可以通过检查URL是否有查询部分或片段部分来执行此操作,并通过跳过一堆if子句来执行追加,但我想知道如果通过Apache执行此操作是否有干净的方法Commons库或类似的东西.
http://example.com 将会 http://example.com?name=John
http://example.com#fragment 将会 http://example.com?name=John#fragment
http://example.com?email=john.doe@email.com 将会 http://example.com?email=john.doe@email.com&name=John
http://example.com?email=john.doe@email.com#fragment 将会 http://example.com?email=john.doe@email.com&name=John#fragment
我之前已多次运行此场景,并且我希望在不破坏URL的情况下执行此操作.
Python可以像这样乘以字符串:
Python 3.4.3 (default, Mar 26 2015, 22:03:40)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'my new text is this long'
>>> y = '#' * len(x)
>>> y
'########################'
>>>
Run Code Online (Sandbox Code Playgroud)
Golang可以做某事吗?
我有一块结构.
type Config struct {
Key string
Value string
}
// I form a slice of the above struct
var myconfig []Config
// unmarshal a response body into the above slice
if err := json.Unmarshal(respbody, &myconfig); err != nil {
panic(err)
}
fmt.Println(config)
Run Code Online (Sandbox Code Playgroud)
这是这个的输出:
[{key1 test} {web/key1 test2}]
Run Code Online (Sandbox Code Playgroud)
如何搜索此数组以获取元素在哪里key="key1"?
我无法理解如何正确地确保nil在这种情况下不存在某些事情:
package main
type shower interface {
getWater() []shower
}
type display struct {
SubDisplay *display
}
func (d display) getWater() []shower {
return []shower{display{}, d.SubDisplay}
}
func main() {
// SubDisplay will be initialized with null
s := display{}
// water := []shower{nil}
water := s.getWater()
for _, x := range water {
if x == nil {
panic("everything ok, nil found")
}
//first iteration display{} is not nil and will
//therefore work, on the second iteration …Run Code Online (Sandbox Code Playgroud) 我想打印string一个uint64,但没有组合strconv,我用的是工作方法.
log.Println("The amount is: " + strconv.Itoa((charge.Amount)))
Run Code Online (Sandbox Code Playgroud)
给我:
cannot use charge.Amount (type uint64) as type int in argument to strconv.Itoa
我怎么打印这个string?
func main() {
var data = map[string]string{}
data["a"] = "x"
data["b"] = "x"
data["c"] = "x"
fmt.Println(data)
}
Run Code Online (Sandbox Code Playgroud)
它运行.
func main() {
var data = map[string][]string{}
data["a"] = append(data["a"], "x")
data["b"] = append(data["b"], "x")
data["c"] = append(data["c"], "x")
fmt.Println(data)
}
Run Code Online (Sandbox Code Playgroud)
它也运行.
func main() {
var w = map[string]string{}
var data = map[string]map[string]string{}
w["w"] = "x"
data["a"] = w
data["b"] = w
data["c"] = w
fmt.Println(data)
}
Run Code Online (Sandbox Code Playgroud)
它再次运行!
func main() {
var data = map[string]map[string]string{}
data["a"]["w"] = "x"
data["b"]["w"] = "x" …Run Code Online (Sandbox Code Playgroud) 我正在使用Go-ping(https://github.com/sparrc/go-ping)golang库来进行无特权的ICMP ping.
timeout := time.Second*1000
interval := time.Second
count := 5
host := p.ipAddr
pinger, cmdErr := ping.NewPinger(host)
pinger.Count = count
pinger.Interval = interval
pinger.Timeout = timeout
pinger.SetPrivileged(false)
pinger.Run()
stats := pinger.Statistics()
latency = stats.AvgRtt // stats.AvgRtt is time.Duration type
jitter = stats.StdDevRtt// stats.StdDevRtt is time.Duration type
Run Code Online (Sandbox Code Playgroud)
从运行它开始,我得到延迟(以毫秒为单位)和抖动(以微秒为单位).我想要两个相同的单位让我们说毫秒,所以当我做jitter = stats.StdDevRtt/1000或jitter = jitter/1000(将微秒转换为毫秒)时,我得到的是以纳秒为单位的抖动:(.是否有任何方法可以获得相同的单位毫秒的延迟和抖动.