我有一个Mutable的对应列表,我想减少第一个条目的值,所以我的条件是我的通过(更改):
while(n > 0) {
if(sibice[i].first > 0) {
sum += sibice[i].second
//sibice[i].first-- will not compile
n--
} else i++
}
Run Code Online (Sandbox Code Playgroud)
但是Pair类不允许我这样做,除了创建我自己的对之外还有其他任何解决方法,为什么会这样呢?
package one;
public class A {
protected int first;
protected static int second;
}
Run Code Online (Sandbox Code Playgroud)
package two;
import one.A;
public class B extends A {
public void someMethod() {
this.first = 5; //works as expected
B.second = 6; //works
A a = new A();
// a.first = 7; does not compile
//works just fine, but why?
a.second = 8;
A.second = 9;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不对静态字段应用相同的限制,它背后的想法是什么?
我正在创建一个文件,然后使用系统调用打开它。
创建:
mov rax, 85
mov rdi, path
mov rsi, 400
syscall
Run Code Online (Sandbox Code Playgroud)
开幕:
mov rax, 2
mov rdi, path
mov rsi, 2 ; I found somewhere that I should use 400 to append
syscall
mov r8, rax ; save the file handle
Run Code Online (Sandbox Code Playgroud)
写作:
mov rax, 1
mov rdi, r8
mov rsi, output
mov rdx, length
syscall
Run Code Online (Sandbox Code Playgroud)
完成这些步骤后,我自然地关闭了文件,但是我无法附加到它,每次执行这些操作时它只会重写文件的内容,但是例如我计算了前 n 个素数并将它们存储在一些内存块,现在我想在文件中写下这 n 个素数,但如果不附加更多行,我就无法这样做。
还有一种方法可以不为writein指定字符串的长度rdx,而是让字符串以/0?