[Java in NetBeans] Lesson 14. ArrayList and Collections

时间:2021-04-26 21:23:31

这个课程的参考视频和图片来自youtube

主要学到的知识点有:

1. Collectioncontainer that contians objects.

2. Difference between Collection and Array 

collection array example
Dynamic siized fixed sized int[] grades = new int[20]
cannot  Primitive types ArrayList<integer> grades = new ArrayList<>();

note: int =>  Integer

double => Double

3. ArrayList: an implemnetation of a collection.

  • Create a ArrayList. (assume that we have a Card class has number and suit)
ArrayList<Card> cards = new ArrayList<>():
  • Add element in the ArrayList
cards.add(new Card(8, Diamond));
cards.add(new Card(6, Diamond));
  • Remove element in the ArrayList
Card temp = cards.remove(); // will move the first elemnt in the ArrayList
  • Set element in the ArrayList
cards.set(, new Card(, Diamond)); // will set the first elemnt in the ArrayList
  • Get element in the ArrayList
cards.get(); // will get the first elemnt in the ArrayList
  • Insert element in the ArrayList
cards.add(, new Card(, Diamond)); // will add Diamond 1 at the first elemnt in the ArrayList