struct encrypt_stream : public std::stringbuf
{
int sync() { encrypt_buffer(); flush_to_device(); }
...
private:
void encrypt_buffer();
void flush_to_device();
};
Run Code Online (Sandbox Code Playgroud)
我想编写这个类来加密文本数据。每次stringbuf刷新该对象时,sync都会调用该对象并将缓冲区的加密版本发送到设备。flush加密文本很大程度上取决于调用的时刻。是否存在flush不需要用户请求就可以调用该对象的棘手情况?
我知道收到flush后会被调用std::endl,但是还有其他我不知道的情况吗?
我浏览了String和StringBufferAPI,当您调用该substring方法时,两者在内部的工作方式似乎相同。两者都有对原始字符数组 ( char[] original)的引用,因此两者都使用相同的空间。那为什么是StringBuffer首选呢?那么 stringbuffer.substring(2,4) 与 string.substring(2,4) 有何不同。
StringBuffer 的子串方法:
public String substring(int start, int end) {
return new String(value, start, end - start);
}
Run Code Online (Sandbox Code Playgroud)
字符串的子串方法:
public String substring(int beginIndex, int endIndex) {
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
Run Code Online (Sandbox Code Playgroud)
两个子字符串变体然后调用 String 构造函数:
public String(char value[], int offset, int count) {
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
Run Code Online (Sandbox Code Playgroud)
它调用以下 Arrays 类的方法:
public static …Run Code Online (Sandbox Code Playgroud) 我有一个StringBuffer , strbuf=161656070200000000202020202001000000009E52;.
我想转换成一个如下所示的字节数组:
byte[] byte_array=new byte[]
{(byte)0x16,(byte)0x16,(byte)0x56,(byte)0x07,
(byte)0x02,(byte)0x00,(byte)0x00,(byte)0x00,
(byte)0x00,(byte)0x20,(byte)0x20,(byte)0x20,
(byte)0x20,(byte)0x20,(byte)0x01,(byte)0x00,
(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x9E,(byte)0x52};
Run Code Online (Sandbox Code Playgroud)
我需要转换strbuf为hex字符串然后吗 getBytes()?解决这个问题的正确方法是什么?
以下代码不起作用.任何人都可以告诉我以下代码有什么问题.逻辑上它应该工作......
package assignments;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IsPalindrome {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a Word:");
StringBuffer sb1 = new StringBuffer(br.readLine());
StringBuffer sb2 = new StringBuffer(sb1);
sb1.reverse();
if(sb2.equals(sb1))
System.out.println("Palindrome");
else
System.out.println("Not a Palindrome");
}
}
Run Code Online (Sandbox Code Playgroud) 您好我正在使用StringBuffer和StringBuilder,这是我写的一个小程序,以帮助我理解它是如何工作的.然而,一些奇怪的东西与Stringbuffer无关,而是我的for循环中的多if if else语句.
我的小代码:
public class c
{
public static void main(String args[])
{
StringBuffer infixB = new StringBuffer();
String infix = "3/8";
for(int i = 0; i < infix.length(); i++)
{
infixB.append(infix.charAt(i));
}
infixB.append(')');
System.out.println("Your current infix expression: "+infixB.toString());
//go through the 'infixB' 1 position at a time
for(int i = 0; i < infixB.length(); i++)
{
if(infixB.charAt(i) == '3')
{
System.out.println("Digit 3 at: " +i);
}
else if(infixB.charAt(i) == '/')
{
System.out.println("Operator at: "+i);
}
else if(infixB.charAt(i) == …Run Code Online (Sandbox Code Playgroud) 我是java技术的新手,可能是一个简单的问题,但我很困惑.我知道java中没有引用调用的概念.它只使用按值调用,即将副本传递给另一个方法
请检查以下代码的输出
public class Test {
public static void main(String[] args) {
StringBuffer a = new StringBuffer ("A");
StringBuffer b = new StringBuffer ("B");
System.out.println("Before Operate"+a + "," +b);
operate (a,b);
System.out.println(a + "," +b);
}
static void operate (StringBuffer x, StringBuffer y) {
x.append(y);
y = x.append("C");
y.append("D");
}
}
Run Code Online (Sandbox Code Playgroud)
以上代码的输出为:ABCD,B(Second Syso)
我不知道为什么这会把所有东西都追加到b中.我google了很多,但它只会造成混乱.如果有人完全详细说明,那将会很有帮助.
提前致谢
PS: - 如果已经在这个论坛上被问到,请给我一个链接,因为我无法找到它.
所以我需要编写一个方法,它允许我找到下一个数字而不使用除String和StringBuffer类之外的任何东西(所以没有解析或任何东西).
当我运行我的代码时,除了最后一个输入(String k)之外,一切似乎都能正常工作.它会导致while循环中的outofbounds错误.但我不明白它是如何做到的,因为我的布尔值应该是假的(我认为因为它只包含九个)它不应该首先进入te循环.
public class Palindroom {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="347"
+ ""; System.out.println("s "+increment(s));
String p="199919";System.out.println(" p "+increment(p));
String d="199999";System.out.println( " d "+increment(d));
String k ="999999";System.out.println(" k "+increment(k));
}
static String increment (String s) {
StringBuffer sb = new StringBuffer (s);
char laatst = sb.charAt(sb.length()-1);
int laatste = sb.length()-1;
if(laatst == '9') {
int a = 1;
boolean nine = false;
for (int i = 0; i < sb.length(); i++) …Run Code Online (Sandbox Code Playgroud) 我在项目中看到了java代码,它从各种参数构造一个文件名.java代码是这样的
String file = null;
String fileDirSeperator = System.getProperty("file.separator");
String pwd = System.getProperty("user.dir");
file = pwd + fileDirSeperator + "properties" + fileDirSeperator + folder_name + file_name;
Run Code Online (Sandbox Code Playgroud)
我想知道我是否应该StringBuffer代替你String.我知道将任何内容附加到String会创建新对象,所以我想我应该StringBuffer.
我可以知道你的建议吗?
也,
file = pwd + fileDirSeperator + "properties" + fileDirSeperator + folder_name + file_name;
Run Code Online (Sandbox Code Playgroud)
在上面的文件名构造中,是否会创建6个对象(每个变量一个)?
String s="";
StringBuffer sb2=new StringBuffer("hi");
StringBuffer sb3=new StringBuffer(sb2);
StringBuffer sb4=sb3;
if(sb2.equals(sb3))
{
System.out.println("sb2.equals(sb3):"+sb2.equals(sb3));
s+="2 ";
}
if(sb3.equals(sb4))
{
System.out.println("sb3.equals(sb4):"+sb3.equals(sb4));
s+="3 ";
}
Run Code Online (Sandbox Code Playgroud)
为什么首先是if给出false而第二个给出true,因为s2,s3,s4都是StringBuffer对象而String Buffer没有equal()方法?
奇怪的行为 StringBuffer.append():
javatpoint状态:
如果字符数从当前容量增加,则>>将容量增加(oldcapacity * 2)+2。
可以通过以下代码进行演示:
class test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("abcdef"); // 16;
System.out.println(sb.capacity());
sb.append("1234561711"); // 34
System.out.println(sb.capacity()); // 34
}
}
Run Code Online (Sandbox Code Playgroud)
但是,它在以下代码中显示了奇怪的行为:
class test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("abcdef"); // 16;
System.out.println(sb.capacity());
sb.append("12345617111111111111111111111111111111"); // 44 total 44 character in sb, so capacity should be 70 as it goes from 16, 34, 70, 142 etc.
System.out.println(sb.capacity()); // 34
} …Run Code Online (Sandbox Code Playgroud) stringbuffer ×10
java ×9
string ×3
bytearray ×1
c++ ×1
if-statement ×1
object ×1
std ×1
substring ×1
tostring ×1