Java String array: is there a size of method?

后端 未结 11 2017
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 03:38

I come from a php background and in php, there is an array_size() function which tells you how many elements in the array are used.

Is there a similar

11条回答
  •  难免孤独
    2020-11-27 03:50

    Also, it's probably useful to note that if you have a multiple dimensional Array, you can get the respective dimension just by appending a '[0]' to the array you are querying until you arrive at the appropriate axis/tuple/dimension.

    This is probably better explained with the following code:

    public class Test {
        public static void main(String[] args){
            String[][] moo = new String[5][12];
    
            System.out.println(moo.length); //Prints the size of the First Dimension in the array
            System.out.println(moo[0].length);//Prints the size of the Second Dimension in the array
        }
    
    }
    

    Which produces the output:

    5
    12
    

提交回复
热议问题