gpt4 book ai didi

java - Java 中的计数和终止字符串

转载 作者:行者123 更新时间:2023-11-30 04:41:14 25 4
gpt4 key购买 nike

Two methods of implementing a string. A counted string explicitly records its length. The terminated string’s length is determined by an end-of-string mark.

谁能给出一个java中计数字符串和终止字符串的例子吗?

最佳答案

CountedString {
char[] string;
int length;

int getLength() {
return length;
}
}


TerminatedString {
char[] string;
final static char TERMINATOR = '$';
int getLength() {
for (int i = 0; i < string.length; i++) {
if (string[i] == TERMINATOR) return i;
}
}
}

关于java - Java 中的计数和终止字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com