How to find length of a string array?

前端 未结 8 1169
温柔的废话
温柔的废话 2020-12-13 19:10

I am having a problem with the following lines where car is a String array which has not been initialized/has no elements.

String c         


        
相关标签:
8条回答
  • 2020-12-13 19:51

    Well, in this case the car variable will be null, so dereferencing it (as you do when you access car.length) will throw a NullPointerException.

    In fact, you can't access that variable at all until some value has definitely been assigned to it - otherwise the compiler will complain that "variable car might not have been initialized".

    What is it you're trying to do here (it's not clear to me exactly what "solution" you're looking for)?

    0 讨论(0)
  • 2020-12-13 19:51

    I think you are looking for this

    String[] car = new String[10];
    int size = car.length;
    
    0 讨论(0)
提交回复
热议问题