I'm making a calculator and I have converted a String to a char array using
我正在制作一个计算器,我已经将一个String转换为一个char数组
char[] b = inputString.toCharArray();
(inputString is my String variable)
(inputString是我的String变量)
Because the String is an input I don't know how large the array will be. Is there an inbuilt method that allows you to find the number of elements in the array? Thanks in advance.
因为String是一个输入,我不知道数组有多大。是否有内置方法允许您查找数组中的元素数量?提前致谢。
6 个解决方案
#1
18
You can use b.length
to find out how many characters there are.
您可以使用b.length来查找有多少个字符。
This is only the number of characters, not indexing, so if you iterate over it with a for
loop, remember to write it like this:
这只是字符数,而不是索引,所以如果你用for循环迭代它,记得像这样写:
for(int i=0;i < b.length; i++)
Note the <
(not a <=
). It's also important to note that since the array isn't a class, .length
isn't a function, so you shouldn't have parenthesis afterward.
注意<(不是<=)。同样重要的是要注意,由于数组不是类,因此.length不是函数,所以之后不应该有括号。
#2
12
Don't listen to any of these guys, here, try this!
不要听任何这些家伙,在这里试试吧!
static int length(final char[] b) {
int n = 0;
while (true) {
try {
int t = b[n++];
} catch (ArrayIndexOutOfBoundsException ex) {
break;
}
}
return n;
}
(Just kidding... try b.length
.)
(开玩笑吧......试试b.length。)
#3
1
b.length
is the way to find length of array
b.length是查找数组长度的方法
length
property determines size of the array.
length属性确定数组的大小。
#4
0
The above answers work. Also, if for some reason you need to know the array size before you actually create the array (for pre-processing or some such activity), you can get the string's length (which will be equal to that of the array).
以上答案有效。此外,如果由于某种原因,您需要在实际创建数组之前知道数组大小(用于预处理或某些此类活动),您可以获得字符串的长度(将等于数组的长度)。
inputString.length()
#5
0
Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length
. For example, myList.length
returns 10.
创建数组后,其大小是固定的。它无法改变。您可以使用arrayRefVar.length找到它的大小。例如,myList.length返回10。
#6
-1
b.length; is very much handy and very much useful to find the length of char[] array:)
b.length个;找到char []数组的长度非常方便,非常有用:)
#1
18
You can use b.length
to find out how many characters there are.
您可以使用b.length来查找有多少个字符。
This is only the number of characters, not indexing, so if you iterate over it with a for
loop, remember to write it like this:
这只是字符数,而不是索引,所以如果你用for循环迭代它,记得像这样写:
for(int i=0;i < b.length; i++)
Note the <
(not a <=
). It's also important to note that since the array isn't a class, .length
isn't a function, so you shouldn't have parenthesis afterward.
注意<(不是<=)。同样重要的是要注意,由于数组不是类,因此.length不是函数,所以之后不应该有括号。
#2
12
Don't listen to any of these guys, here, try this!
不要听任何这些家伙,在这里试试吧!
static int length(final char[] b) {
int n = 0;
while (true) {
try {
int t = b[n++];
} catch (ArrayIndexOutOfBoundsException ex) {
break;
}
}
return n;
}
(Just kidding... try b.length
.)
(开玩笑吧......试试b.length。)
#3
1
b.length
is the way to find length of array
b.length是查找数组长度的方法
length
property determines size of the array.
length属性确定数组的大小。
#4
0
The above answers work. Also, if for some reason you need to know the array size before you actually create the array (for pre-processing or some such activity), you can get the string's length (which will be equal to that of the array).
以上答案有效。此外,如果由于某种原因,您需要在实际创建数组之前知道数组大小(用于预处理或某些此类活动),您可以获得字符串的长度(将等于数组的长度)。
inputString.length()
#5
0
Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length
. For example, myList.length
returns 10.
创建数组后,其大小是固定的。它无法改变。您可以使用arrayRefVar.length找到它的大小。例如,myList.length返回10。
#6
-1
b.length; is very much handy and very much useful to find the length of char[] array:)
b.length个;找到char []数组的长度非常方便,非常有用:)