[Java in NetBeans] Lesson 14. ArrayList and Collections

这个课程的参考视频和图片来自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(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

猜你喜欢

转载自www.cnblogs.com/Johnsonxiong/p/10147291.html
今日推荐