英文:
Is there a way in Java for taking input directly in charArray?
问题
我正在使用 BufferedReader 来获取输入。
String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
在程序的下一个部分中,我可能会操作或更改上述字符串的字符,因为在 Java 中字符串是不可变的,所以我要么将其转换为 StringBuilder,要么转换为 charArray。我想避免将字符串转换为 StringBuilder 或 charArray 的 O(n) 转换。是否有直接将输入获取到 charArray 或 StringBuilder 中的方法?
英文:
I am using BufferedReader for taking input.
String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
Here, in next part of the program, I may manipulate or change characters of above String, so as strings are immutable in Java, I either convert it to StringBuilder or charArray. I want to avoid that O(n) conversion for converting String to StringBuilder or charArray.
Is there any way for directly taking input in charArray or StringBuilder?
答案1
得分: 2
如果你知道有多少个字符,你可以像这样将整个流读入字符数组:
char[] chars = new char[nosChars];
BufferedReader br = new BufferedReader(source);
int nosRead = br.read(chars, 0, nosChars);
// 然后再进行另一次读取以检查是否已经读取了所有内容...
如果输入实际上是一个文件,并且文件编码是一种允许你通过文件大小(以字节为单位)计算字符数的编码,那么上述方法可以生效。
然而,如果你不知道 nosChars
是多少,或者没有一个可靠但合理的过高估计,那么你所能做的任何事情要么会涉及额外的复制步骤,要么会在流上(以某种方式)进行两次传递,以计算出 char[]
需要多大。两者的时间复杂度都是 O(N)
。
但我怀疑你会发现将输入读入char[]
并不是一个好主意。在char[]
(或包装在char[]
周围的StringBuilder
)中编辑数据,对于每个插入或删除的字符,时间复杂度可能是O(N)
...而N
是整个输入文件的长度。对于许多编辑操作来说,将其表示为String
对象的行列表可能会更有效率。
最后,你应该遵循关于优化的标准建议。在以下情况下再进行优化:
- 你知道存在实际的性能问题
- 你知道性能问题出现在哪里
- 你有一种有效衡量你的优化是否有效的方法。
英文:
It you know how many characters there are, you can read the entire stream into a char array like this:
char[] chars = new char[nosChars];
BufferedReader br = new BufferedReader(source);
int nosRead = br.read(chars, 0, nosChars);
// Then do another read to check that you have read everything ...
If the input is actually a file and the file encoding is one that allows you to calculate the character count from the file size in bytes, then the above can be made to work.
However, if you don't know what nosChars
is, or don't have a reliable but reasonable overestimate, then anything you can do will either involve an extra copying step or passing over the stream (somehow) twice to work out how big the char[]
needs to be. Both are O(N)
.
But I suspect you will find that reading the input into a char[]
is a bad idea. Editing data in a char[]
(or a StringBuilder
wrapped around a char[]
) is liable to be O(N)
for each character you insert or delete ... and N
is the length of the entire input file. For many edits, it will be more efficient to start with a list of lines represented as String
objects.
Finally, you should heed the standard advice about optimization. Don't optimize until:
- you know there is a real performance problem
- you know where the performance problem is
- you have a good way of measuring whether your optimization is effective.
答案2
得分: 0
试试这个。告诉我它是否起作用!
Scanner sc = new Scanner(System.in);
//假设你想要存储5个字母
char [] lettrs = new char[5];
for (int i = 0; i < lettrs.length; i++){
//这个消息会在循环完成之前一直显示
System.out.println("写一个字母");
//获取输入
char c = sc.next().charAt(0);
//将输入的字母存储到数组中
lettrs[i] = c;
}
//这个循环是为了打印你存储在数组中的字母
for (int i = 0; i < lettrs.length; i++) {
System.out.println(lettrs[i]);
}
使用BufferedReader和动态字符数组
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("写一个字母");
char c = (char)br.read();
char [] lettrs = {c};
英文:
Try this. Tell me if it's worked!
Scanner sc = new Scanner(System.in);
//Say you want store 5 letters
char [] lettrs = new char[5];
for (int i = 0; i < lettrs.length;i++){
//This message will show until the loop finish
System.out.println("Write one letter");
//Taking input
char c = sc.next().charAt(0);
//storing input letter to array
lettrs[i] = c;
}
//This for is to print the letters you stor in to the array
for (int i = 0; i <lettrs.length ; i++) {
System.out.println(lettrs[i]);
}
With BufferedReader and dynamic char array
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Write a letter");
char c = (char)br.read();
char [] lettrs = {c};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论