zoukankan      html  css  js  c++  java
  • Java中length、length()、size()的区别

    原文链接:https://blog.csdn.net/qq_33236248/article/details/79884874

    首先区分一下length和length();

    length不是方法,是属性,数组的属性;

    public static void main(String[] args) {
        int[] intArray = {1,2,3};
        System.out.println("这个数组的长度为:" + intArray.length);
    }

    length()是字符串String的一个方法;

    public static void main(String[] args) {
        String str = "HelloWorld";
        System.out.println("这个字符串的长度为:" + str.length());
    }

    进入length()方法看一下实现

    private final char value[];
     
    public int length() {
            return value.length;
        }

    注释中的解释是

    @return     the length of the sequence of characters represented by this object.

    即由该对象所代表的字符序列的长度,所以归根结底最后要找的还是length这个底层的属性

    size()方法,是List集合的一个方法;

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        System.out.println("这个list的长度为:" + list.size());
    }

    在List的方法中,是没有length()方法的;

    也看一段ArrayList的源码

    private final E[] a;
     
    ArrayList(E[] array) {
           if (array==null)
                 throw new NullPointerException();
           a = array;
    }
     
    public int size() {
           return a.length;
    }

    由这段就可以看出list的底层实现其实就是数组,size()方法最后要找的其实还是数组的length属性;

    另外,除了List,Set和Map也有size()方法,所以准确说size()方法是针对集合而言。

    总结:

    length——数组的属性;

    length()——String的方法;

    size()——集合的方法;

    谨记。


  • 相关阅读:
    51nod乘积之和
    Dell服务器安装OpenManage(OMSA)
    Nginx反向代理PHP
    搭建haproxy
    108. Convert Sorted Array to Binary Search Tree
    60. Permutation Sequence
    142. Linked List Cycle II
    129. Sum Root to Leaf Numbers
    118. Pascal's Triangle
    26. Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/doyi111/p/11719540.html
Copyright © 2011-2022 走看看