How to find length of a string array?

前端 未结 8 1167
温柔的废话
温柔的废话 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:42

    In Java, we declare a String of arrays (eg. car) as

    String []car;
    String car[];
    

    We create the array using new operator and by specifying its type:-

    String []car=new String[];
    String car[]=new String[];
    

    This assigns a reference, to an array of Strings, to car. You can also create the array by initializing it:-

    String []car={"Sedan","SUV","Hatchback","Convertible"};
    

    Since you haven't initialized an array and you're trying to access it, a NullPointerException is thrown.

提交回复
热议问题