Friday, November 18, 2016

ArrayList


When programming, we often need to use arrays. Besides creating an array, the most common method we use so far, we can also use ArrayList as an alternative way. The ArrayList class belongs to java.util, so we need to use import if we want to use ArrayList. The following are some advantages of using ArrayList instead of standard arrays.

To create an array, you should first know how many elements you would include in this array, because once the array is created, you cannot change its length without making a new reference. However, an ArrayList can either grows and shrinks as you need. Although you also need to create an ArrayList with an initial size, this size can easily increase as the number of elements exceeds the size, or decrease when you remove an element from the ArrayList. Using ArrayList, you won’t bother to keep track of the last slot in use, because the last slot in an ArrayList must be list.size()-1;



As what we learn in the class, ArrayList is generic. Thus, the parameter of ArrayList must be nonprimitive type. This means that if we create an ArrayList class, we should write code like this:
public class ArrayList<Integer> We cannot write such code as public class ArrayList<int>

---------------------------------------------------------------------------------

Writing References:
1.Barron's AP Computer Science
2.https://www.tutorialspoint.com/java/java_arraylist_class.htm
Picture References:
1.http://www.trustingeeks.com/wp-content/uploads/2014/12/Arraylist-example-in-java.png
2.http://www.codeproject.com/KB/vb/ArrayList2File/multiDimArrayList.png

1 comment:

  1. This is really a good blog that we can use for our programming. But can you write more about what java.util is because I think we import it quite often but still haven't talked much about it.

    ReplyDelete