# 備忘-15號 ###### tags: `筆記本` *若覺得內容看起來有怪怪的地方請吐槽! *請善用左上項目來跳轉! --------------------------------------------------- 進階for迴圈(foreach)取陣列值 --------------------------------------------------- 1. 無須知道陣列大小 2. 無需知道陣列內物件位置 3. 也適用有實作iterator介面的list、set等 ``` int[] y ={1,2,3,4,5}; for(int item : y){ //item用來接y陣列每迴圈產生的值 System.out.println(item); } ``` [foreach和iterator間的關聯](https://openhome.cc/Gossip/DesignPattern/IteratorPattern.htm) --------------------------------------------------- 三元運算 --------------------------------------------------- `a? b : c` 意義同下 ``` if(條件a){ 執行b ; } else{ 執行c ; } ``` 三元運算聽說(?)效能較好,方便把簡單的判斷塞進printf、println裡 但使用時必須有b和c,這代表條件a結束後若沒有要執行b或c就只能用if...else --------------------------------------------------- 陣列(Array) --------------------------------------------------- 1. 建立後大小無法改變 2. 無法以key值指定位置 3. 無法處理「不重複」的需求 先定義Employee型態內有那些屬性 ``` public class Employee { int empno; String name; } ``` main方法內宣告 ``` Employee[] emps = new Employee[3] ; //emps內共有三個位置,呼叫時使用[0],[1],[2] Employee emp1 = new Employee(); emp1.empno = 001 ; emp1.name = "AAA" ; emps[0] = emp1 ; //將emp1於stack的記憶體位址傳入stack區emps的第一個位置 emps[2] = new Employee() ; //把於heap產生的新Employee空間位址傳入stack區emps的第三個位置 emps[2].empno = 003 ; //直接於emps[2]指向的heap區空間產生物件 emps[2].name = "BBB" ; int[] y ={1,2,3,4,5}; //這樣產生也可以,由於有int限制內容物就不能亂放了 ``` --------------------------------------------------- ArrayList --------------------------------------------------- Arraylist ---(implements)---> List ---(extends)---> Collection ---(extends)---> Iterable * 只有Arraylist是class,其他都是interface 1. 物件可重複,以位置區分 2. 物件依加入順序排序,陣列大小自動調整 3. 只能放入物件(由於autoboxing機制,直接輸入基本型別也ok) ``` (省略import) public class TestArrayList { public static void main(String[] args) { // ArrayList list = new ArrayList(); List list = new ArrayList(); //實務上宣告會用大的型態 list.add("Hello"); list.add("World"); list.add(3); //autoboxing機制自動包裝,方便直接輸入數字 // list.add(new Integer(3)); //上一句實際執行的樣子 list.add(0, "World"); //指定位置新增(插隊) System.out.println(list.get(0)); System.out.println("size = "+list.size()); //list總大小 list.remove(1); //移除"Hello" System.out.println("size after remove = "+list.size()); System.out.println("-----for迴圈-----"); for(int i=0 ; i < list.size(); i++) { System.out.println(list.get(i)); //透過位置i取出內容物 } System.out.println("-----使用Iiterator-----"); //hasNext、next方法位於類別Iterator Iterator iterator = list.iterator(); //類別ArrayList裡的iterator()會幫我們new好物件,因此可以寫成上面這樣 while(iterator.hasNext()) { //檢查游標下方有無物件,有則true Object item = iterator.next(); //游標向下移動並取出陣列list內物件 System.out.println(item); } System.out.println("-----進階型for迴圈-----"); for(Object item : list) { //無須知道陣列位置、大小取出內容物 System.out.println(item); } } } ``` Q :「Iterator iterator = list.iterator()」這行理解不能? A : eclipse內F3後發現類別ArrayList裡的iterator()會幫我們new好,因此寫成這樣沒問題。 --------------------------------------------------- equals、hashcode --------------------------------------------------- * 應用在set、map檢查物件是否重複 * 兩物件使用equals()測試為true則hashcode必須相同 以下為equals、hashcode相關處理(main方法) ``` public class TestEquals { public static void main(String[] args) { Employees e1 = new Employees("001"); Employees e2 = new Employees("001"); System.out.println(e1==e2); //由於new了兩個,stack位置一定不同(回傳false) System.out.println(e1.equals(e2)); //於equals上按F3發現,物件比較預設使用==,因此與上一條完全相同 System.out.println("001".equals("001")); //若輸入為string則有不同比較方法 System.out.println("el "+e1.hashCode()); System.out.println("e2 "+e2.hashCode()); System.out.println("001".hashCode()); } } ``` equals、hashcode相關處理 ***eclipse滑鼠右鍵source選單可自己產生equals()、hashcode()** ``` public class Employees { private String id; public Employees(String id) { this.id = id; } // @Override // public boolean equals(Object obj) { // return super.equals(obj); //此為object裡的equals(),為==方式 // } @Override public boolean equals(Object obj) { if(obj instanceof Employees) { //如果有Employee型態才做進一步比較 Employees emp = (Employees)obj; //為拿到id需將object型態的obj轉Employees if(this.id.equals(emp.id)) { //id一樣才回傳true return true; } else { return false; } } return false; } @Override public int hashCode() { //滿足兩物件equal時hashcode相同的條件 return id.hashCode(); } } ``` --------------------------------------------------- hashcode用在set、map裡 --------------------------------------------------- 以add物件進HashSet為例 :  1. 加入者和既存者間比較是否有相同hash 2. 有->接著將hash相同的兩者以「o1.equals(o2)」再次比較,回傳false則放入 無->直接將加入者放入 **不符合equals、hashcode使用規則會發生什麼事?**  1. 加入者和既存者間比較是否有相同hash 2. 沒有相同hash,因此將加入者直接放入,不合set使用原則(物件不重複)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up