我似乎无法理解 Java 如何将流操作组合到流管道中。
执行以下代码时
public
static void main(String[] args) {
StringBuilder sb = new StringBuilder();
var count = Stream.of(new String[]{"1", "2", "3", "4"})
.map(sb::append)
.count();
System.out.println(count);
System.out.println(sb.toString());
}
Run Code Online (Sandbox Code Playgroud)
控制台只打印4
. 该StringBuilder
对象仍然具有值""
。
当我添加过滤操作时: filter(s -> true)
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
var count = Stream.of(new String[]{"1", "2", "3", "4"})
.filter(s -> true)
.map(sb::append)
.count();
System.out.println(count);
System.out.println(sb.toString());
}
Run Code Online (Sandbox Code Playgroud)
输出更改为:
4
1234
Run Code Online (Sandbox Code Playgroud)
这个看似多余的过滤操作如何改变组合流管道的行为?
试图通过单击来更改单元格的颜色。
单元格通常是灰色的,第一次点击它们应该变成红色。当我单击红色单元格时,它应该再次变为灰色。
function changeColor(cell) {
var red = '#FE2E2E';
var grey = '#E6E6E6';
if (cell.style.backgroundColor == grey) {
cell.style.backgroundColor = red;
} else {
if (cell.style.backgroundColor == red) {
cell.style.backgroundColor = grey;
}
};
};
Run Code Online (Sandbox Code Playgroud)
#table tr td {
width: 20px;
height: 50px;
cursor: pointer;
background-color: #E6E6E6;
border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)
<table class="table table-bordered" id="table">
<tbody>
<tr>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
<td onclick="changeColor(this)"></td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我也试过用.style.bgColor
,rgb
并if (cell.style.backgroundColor ===
但这也不能工作。单元格背景颜色的值是 …
对于我的网站,我有两个不同的 CSS 文件。一种用于以深色主题显示网站,另一种用于以浅色主题显示网站。
现在我想让用户在两个主题之间切换。但我不知道如何实现这个。我不想创建两个 HTML 文件,唯一的区别是一个文件有一个指向浅色主题的链接标签,另一个有一个指向深色主题的链接标签。
最好的解决方案是用户只需单击按钮即可更改主题,而无需重新加载。
这可能吗?
谢谢!
决定接受这个答案。谢谢@void
尝试编译
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt()++;
}
}
Run Code Online (Sandbox Code Playgroud)
给出错误java: unexpected type; required: variable; found: value
。
使用+ 1
而不是++
解决此错误。
我知道区别是“将变量的值加1”和“将值增加1”。
但是我从没碰到能带来改变的代码。有人可以解释吗?