7 1 1 public static void mainString args
7. 1. 1 配列リストと 連結リストの性能比較 public static void main(String[] args) { long start. Time; //開始時間を保存する long end. Time; //終了時間を保存する //-------配列リストの性能を測定する------//商品種類配列リストを生成する Item. Type. Array. List item. Type. Array. List = new Item. Type. Array. List(); start. Time = System. current. Time. Millis(); //開始時間を測定する //商品種類を 1万件登録する for(int i=0; i<10000; i++){ item. Type. Array. List. add(new Item. Type(1001, "コーラ", 120)); } end. Time = System. current. Time. Millis(); //終了時間を測定する System. out. println("かかった時間は"+ (end. Time - start. Time) + "ミリ秒です"); //-------連結リストの性能を測定する------//商品種類連結リストを生成する Item. Type. Linked. List item. Type. Linked. List = new Item. Type. Linked. List(); start. Time = System. current. Time. Millis(); //開始時間を測定する //商品種類を 1万件登録する for(int i=0; i<10000; i++){ item. Type. Linked. List. add(new Item. Type(1001, "コーラ", 120)); } end. Time = System. current. Time. Millis(); //終了時間を測定する System. out. println("かかった時間は"+ (end. Time - start. Time) + "ミリ秒です"); } 例題7 -1
①ほとんど同じコードが2度 public static void main(String[] args) { long start. Time; //開始時間を保存する long end. Time; //終了時間を保存する 例題7 -1 (Example 7_1. java) //-------配列リストの性能を測定する------//商品種類配列リストを生成する Item. Type. Array. List item. Type. Array. List = new Item. Type. Array. List(); start. Time = System. current. Time. Millis(); //開始時間を測定する //商品種類を 1万件登録する for(int i=0; i<10000; i++){ item. Type. Array. List. add(new Item. Type(1001, "コーラ", 120)); } end. Time = System. current. Time. Millis(); //終了時間を測定する System. out. println("かかった時間は"+ (end. Time - start. Time) + "ミリ秒です"); //-------連結リストの性能を測定する------//商品種類連結リストを生成する Item. Type. Linked. List item. Type. Linked. List = new Item. Type. Linked. List(); start. Time = System. current. Time. Millis(); //開始時間を測定する //商品種類を 1万件登録する for(int i=0; i<10000; i++){ item. Type. Linked. List. add(new Item. Type(1001, "コーラ", 120)); } end. Time = System. current. Time. Millis(); //終了時間を測定する System. out. println("かかった時間は"+ (end. Time - start. Time) + "ミリ秒です"); } 同じ仕事
型が違う n 型が違うので、今までの方法ではまとめら れない //---配列リスト版--Item. Type. Array. List item. Type. Array. List = new Item. Type. Array. List(); ・・・ item. Type. Array. List. add(new Item. Type(1001, "コーラ")); //---連結リスト版--まとめられない Item. Type. Linked. List item. Type. Linked. List = new Item. Type. Linked. List(); ・・・ item. Type. Linked. List. add(new Item. Type(1001, "コーラ"));
②クラス図で表現する Item. Type. Listは Item. Type. Array. Listの スーパークラス Item. Type. Linked. Listは Item. Type. Listの サブクラス
①スーパークラス の変数への代入 //---配列リスト版--Item. Type. Array. List item. Type. Array. List = new Item. Type. Array. List(); //---連結リスト版--Item. Type. Linked. List item. Type. Linked. List = new Item. Type. Linked. List(); Item. Type. Array. List インスタンス item. Type. Array. List Item. Type. Linked. List インスタンス item. Type. Linked. List 基本的に 型が違う変数には代入できない
①スーパークラス の変数への代入 //---配列版--Item. Type. List item. Type. List = new Item. Type. Array. List(); //---連結リスト版--Item. Type. List item. Type. List = new Item. Type. Linked. List(); Item. Type. Array. List インスタンス Item. Type. Linked. List インスタンス item. Type. List スーパークラスの変数には サブクラスのインスタンスも 代入することができる
入っている インスタンスによって切り替わる Item. Type. Array. List インスタンス [0] [1] [2] [3] [4] [5] add() item. Type. List Item. Type. Linked. List インスタンス add() item. Type. List 22(id) 23(id) コーラ ソーダ 102番地 null
③抽象クラスとインターフェイス n メソッド名を間違えるとどうなるか スーパークラス public class Item. Type. List { … //商品種類を表示するメソッド public void display(){ } … } サブクラス public class Item. Type. Array. List extends Item. Type. List{ //商品種類を表示するメソッド public void dispray(){ for(int i=0; i<ARRAY_SIZE; i++){ if(item. Type. Array[i] != null){ System. out. println(item. Type. Arr… } } } … } 違うメソッドと解釈される
「定義」と「実装」 n n 「定義」ーメソッドを定義する 「実装」ーメソッドを実現する 例題7 -3 (Item. Type. List. java) スーパークラス サブクラス public abstract class Item. Type. List { … //商品種類を表示するメソッドの定義 public abstract void display(); public class Item. Type. Array. List extends Item. Type. List{ } //商品種類を表示するメソッド public void display(){ for(int i=0; i<ARRAY_SIZE; i++){ if(item. Type. Array[i] != null){ System. out. println(item. Type. Arr… } } } … } サブクラスで実装しないとコンパイルエラー
どのインスタンスが 入っているのか調べる方法 n 「instanceof」演算子を使います 例題7 -6 (Example 7_6. java) /** * 商品種類リストの性能を測る */ private static void performance. Test(Item. Type. List item. Type. List){ //item. Type. ListがArray. ListなのかLinked. Listなのかを表示する if(item. Type. List instanceof Item. Type. Array. List){ System. out. println("実装クラスの名前:Item. Type. Array. List"); }else if(item. Type. List instanceof Item. Type. Linked. List){ System. out. println("実装クラスの名前:Item. Type. Linked. List"); } } [インスタンス名] instanceof [クラス名] 成立したらtrue, 不成立ならfalse
インスタンスに場合分けを書く スーパークラスで 定義します //商品種類リスト 抽象クラス public abstract class Item. Type. List {. . . //実装クラスの名前を得るメソッド public abstract String get. Name(); サブクラスで実装します //商品種類リスト配列実装クラス public class Item. Type. Array. List extends Item. Type. List{ … //実装クラスの名前を得るメソッド public String get. Name(){ return "Item. Type. Array. List"; } } } 例題7 -6 (Item. Type. List. java) (Item. Type. Array. List. java) (Item. Type. Linked. List. java) //商品種類リスト連結リスト実装クラス public class Item. Type. Linked. List extends Item. Type. List{ … //実装クラスの名前を得るメソッド public String get. Name(){ return "Item. Type. Linked. List"; } }
- Slides: 46