在Java中:
for(int j = 0; j < 6 && j < ((int)abc[j] & 0xff); j++) {
// ...
}
我们如何在Kotlin中制作这个循环?
答案 0 :(得分:9)
我建议使用更实用的方法,比如
(0..5).takeWhile {
it < (abc[it].toInt() and 0xff) // or `as Int` if array is not numeric
}.forEach {
// do something with `it`
}
答案 1 :(得分:4)
如果您不介意创建新的ArrayList实例,可以这样做:
(0..5).takeWhile { it < (abc[it] as Int and 0xff) }
.forEach {
// ...
}
答案 2 :(得分:3)
注意:某些答案中建议的.takeWhile { ... }.forEach { ... }
方法不等同于Java for循环。范围首先使用.takeWhile { ... }
和处理只返回它返回的前缀被迭代。问题是.forEach { ... }
正文的执行不会影响.takeWhile { ... }
的条件,Sequence<T>
已经在第一个项目被调用时已经完成执行。
(see this runnable demo that shows how the behavior is different)
要解决此问题,您可以使用Iterable<T>
。与.takeWhile { ... }
的热切评估相反,它不会使用.forEach { ... }
处理整个项目集,只会在Sequence<T>
处理下一个项目时逐个检查它们项目。请参阅:the difference between Iterable<T>
and Sequence<T>
。
要使用.toSequence()
并实现与Java循环等效的行为,请转换范围(0..5).asSequence()
.takeWhile { it < (abc[it].toInt() and 0xff) }
.forEach {
// Use `it` instead of `j`
}
:
while
或者,只需使用var j = 0
while (j < 6 && j < (abc[j] as Int and 0xff)) {
// do something
j++
}
循环:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<style>
body {
background-color:white;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
#outterBox {
position:relative;
width:500px;
height:320px;
display:flex;
justify-content: top;
flex-direction:column;
background-color:#F0f0f0;
}
#innerBox {
display:flex;
align-self: center;
background-color:white;
width:90%;
height:55%;
margin-top:6px;
flex-direction:column;
}
#headingBox {
display:flex;
align-self: flex-start;
width:100%;
background-color:#f0f0f0;
overflow: hidden;
height:18%;
margin-top:6px;
}
#headingBox > img {
margin-left:-4%;
background-color:#f0f0f0;
}
#notiBar {
display:flex;
align-self: flex-start;
background-color:#a1a1a1;
width:100%;
height:42px;
justify-content: top;
align-items: center;
}
#qqq > span {
font-size:18px;
color:white;
margin:16px;
}
#nnn {
display:flex;
align-self: flex-start;
width:100%;
height:100%;
justify-content: top;
align-items: left;
flex-direction:column;
}
#nnn > span {
font-size:18px;
color:#878787;
margin-top:16px;
margin-bottom:16px;
margin-left:14px;
}
#nnn > a {
display:flex;
background-color:#5683CC;
border:none;
height:36px;
color:white;
font-size:14px;
padding-left:10px;
padding-right:10px;
align-self:center;
align-items:center;
text-decoration:none;
}
#footer {
display:flex;
height:27%;
width:90%;
align-self: center;
}
#footer > span {
align-self: flex-end;
margin-bottom:12px;
color:#878787;
font-size:10px;
}
#u {
text-decoration:none;
color:#81a0d3;
font-size:10px;
}
</style>
</head>
<body>
<div id="outterBox">
<div id="headingBox">
<img src="">
</div>
<div id="innerBox">
<div id="qqq">
<span>New</span>
</div>
<div id="nnn">
<span><span id="aaa">John</span>, xxxxx</span>
<a href="https://www.google.com"><span>Hello</span></a>
</div>
</div>
<div id="footer">
<span> Some footer<a id="u" href="/"> click</a></span>
</div>
</div>
</body>
</html>
答案 3 :(得分:2)
这就是kotlin版本的样子。
var j = 0
while (j < 6 && j < (abc[j] as Int and 0xff)) {
// do something
j++
}
您可以在此处将Java转换为Kotlin。 Try Kotlin。此外,如果您使用IntelliJ,这里有一个链接,可以帮助您从Java转换为Kotlin。 IntelliJ Java to Kotlin.
答案 4 :(得分:-1)
我会将j < ((int)abc[j] & 0xff)
部分移动到循环内的if-test中。然后你可以这样做:
for (j in 0..5) {
if (j < (abc[j].toInt() and 0xff)) {
// Do stuff here
} else break
}
答案 5 :(得分:-1)
这是用于转换的intellij插件的输出:
var j = 0
while (j < 6 && j < abc[j] as Int and 0xff) {
j++
// ...
}