Collection – ArrayList

Collection

Collection refers to the action of grouping things to manage them together

  • ArrayList

ArrayList

ArrayList is a library class in Java. We can access to a library class in Java via the following statement:

屏幕快照 2017-04-04 下午12.08.43.png

Once a class name has been imported from a package in the way above, we can use that class just as if it were one of our own classes. So we use ArrayList at the head of the MusicOrganizer class to define a files field:

屏幕快照 2017-04-04 下午12.09.21.png

The advantage of implementing ArrayList is that someone has invested time and effort to implement something useful, and we are getting access to this functionality almost for free by using this class.

Functionalities: add(int index), get(int index), size(), list(), remove(int index)

屏幕快照 2017-04-04 下午12.20.55.png

Generic classes

Generic classes are classes that require a second class type as a parameter which is used to declare the type of variable inside the class.

  • ArrayList<String>

Diamond Notation

屏幕快照 2017-04-04 下午12.13.04.png

Effect of Removal

4.3
Add two file names - "0" "1"
listFile(0) - "0"
listFile(1) - "1"
removeFile(0)
list- File(0)
The result is "1".

So when we remove one value in the array, the sequence inside the Array is rearranged, and the following value will automatically move one index forward.

4.4-4.6
屏幕快照 2017-04-04 下午8.22.56.png
4.8
屏幕快照 2017-04-04 下午8.33.20.png
4.9
屏幕快照 2017-04-04 下午8.34.04.png
4.10
屏幕快照 2017-04-04 下午8.31.38.png
4.14-4.15
屏幕快照 2017-04-04 下午8.35.32.png
4.16
屏幕快照 2017-04-04 下午8.36.15.png

Personal Reflection

I learned a new class called ArrayList, its functionalities, and advantages. Also, I got reviews of the content about OOP we learned in the last semester. I have almost forgotten everything I learn about programming at first. Although fortunately, those contents can be recalled easily, I should not stop programming otherwise I will forget the syntax again and waste time on recalling them.

 

留下评论