[Java in NetBeans] Lesson 14. ArrayList and Collections
阿新 • • 發佈:2018-12-20
這個課程的參考視訊和圖片來自youtube。
主要學到的知識點有:
1. Collection: container 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(0); // will move the first elemnt in the ArrayList
- Set element in the ArrayList
cards.set(0, new Card(3, Diamond)); // will set the first elemnt in the ArrayList
- Get element in the ArrayList
cards.get(0); // will get the first elemnt in the ArrayList
- Insert element in the ArrayList
cards.add(0, new Card(1, Diamond)); // will add Diamond 1 at the first elemnt in the ArrayList