Finding char array size in Java

I am making a calculator and I converted the string to a char array using

char[] b = inputString.toCharArray();

(inputString is my String variable)

Since String is the input, I don't know how big the array will be. Is there a built-in method that allows you to find the number of elements in an array? Thanks in advance.

+5
source share
6 answers

You can use b.lengthto find out how many characters are.

This is just the number of characters, not indexing, so if you iterate over it with a loop for, remember to write it like this:

for(int i=0;i < b.length; i++)

< ( <=). , , length , .

+17

, !

static int length(final char[] b) {
  int n = 0;
  while (true) {
    try {
      int t = b[n++];
    } catch (ArrayIndexOutOfBoundsException ex) {
      break;
    }
  }
  return n;
}

( ... b.length.)

+11

b.length -

length array.

+1

. , - , ( - ), ( ).

inputString.length()
0

. . , arrayRefVar.length. , myList.length 10.

0

b.length; very convenient and very useful to find the length of the char [] array :)

-1
source

All Articles