Sign in

[Java] [Notes]Array and List difference

interview, participant asked me about that what different from array and list.
mm…. I piece together all of my knowledge instantly. I never think about what different before that. I integrated and recorded the table after interview.

  1. Expandability
    When I initial an Array, I need to give a size and the size can’t be changed. In contrast, List does not must to give a size and I can expand that.
  2. Element
    Array can initial to primitive type and object type. List can initial to object type only.
    Primitive type: int[], string[], and so on…
    Object type: Integer[], String[], and so on…
  3. Generic
    Array can’t use generic, but List can.
  4. Length
    Array get length is length, List is size().
    int[] x=new int[5];
    int length=x.length;
    List<Integer> i=new ArrayList<>();
    int size=i.size();
  5. Assignment Operator
    Array assignment operator is =, List is add().
    int[] x=new int[5];
    x[0]=1; //assignment x[0] is 1
    List<Integer> i=new ArrayList<>();
    i.add(1); //add element