数组列表

An ArrayList provides an alternative way of storing a list of objects and has the following advantages over an array:
  • An ArrayList shrinks and grows as needed in a program ,whereas an array has fixed length that is set when the array is created.
  • In an ArrayLIst list, the last slot is always list.size()-1, whereas in a partially filled array, you ,the programmer,must keep track of the last slot currently in use.
  • For an ArrayList ,you can do insertion or deletion with just a single statement.Any shifting of elements is handled automatically . In an array ,however ,insertion or deletion requires you to write the code than shifts the elements.
  • It is easier to print the elements of an ArrayList than those of an array.For an ArrayLIst list and an array arr ,the statement

        System.out.print(list) ;

   will output the elements of list, nicely formatted in square brackets, with the elements separated by commas. Whereas to print the elements of arr, an explicit plece of code that accesses and prints each element is needed, the statement 

        System.out.print(arr); 

    will produce weird output that includes an @ symbol and the hashcode of arr in hexadecimal.

 

posted @ 2021-11-25 14:23  杭州胡欣  阅读(35)  评论(0编辑  收藏  举报