1 public static void mainString args int x
参照練習問題1 public static void main(String args[]){ int x; int y; x = 3; y = x; y = 15; System. out. println(x); System. out. println(y); }
参照練習問題2 public static void main(String args[]){ Test. Class x; Test. Class y; x = new Test. Class(); x. id = 3; y = x; y. id = 15; System. out. println(x. id); System. out. println(y. id); } class Test. Class { int id; }
②参照とメモリの仕組み(1) メモリ 番地(100) 番地(101) int x; int y; x = 3; y = x; y = 15; System. out. println(x); System. out. println(y); x y 番地 内容 100 0(初期値) 3 101 15 3 0(初期値) 102 103 104
②参照とメモリの仕組み(2) メモリ 番地(100) 番地(101) Test. Class x; Test. Class y; x = new Test. Class(); x. id = 3; y = x; y. id = 15; System. out. println(x. id); System. out. println(y. id); 番地 内容 x 100 0(初期値) 1000 null y 101 1000 null オブジェクト用のメモリ 1000 1001 0(初期値) 15 3
②クラス設計 n 前回同様、ExampleクラスとItem. Type. List クラスを分離します Item. Type. List{ add(); Example{ delete(); main(); search(); } display(); }
(2)Link. Terminalクラス n 引数用オブジェクトLink. Terminalを作り、そ れを渡すことにします //連結リストの始点と終点をもつクラス public class Link. Terminal { Item. Type. List{ Link. Object first; //始点 Link. Object last; //終点 } Example { main(); } add(); delete(); Link. Terminal オブジェクト search(); display(); }
追加(1)一般的な追加 public void add(Link. Terminal link. Terminal, Item. Type add. Item. Type){ 100番地 102番地 108番地 add. Link. data = add. Item. Type; 2221 2222 2223 link. Terminal. last. next = add. Link; コーラ ソーダ 緑茶 102番地 108番地 null first last //追加する連結オブジェクトを生成する Link. Object add. Link = new Link. Object(); null link. Terminal. last = add. Link; } }
追加(2)一番最初の追加 public void insert(Link. Terminal link. Terminal , Item. Type add. Item. Type ){ Link. Object add. Link = new Link. Object(); add. Link. data = add. Item; } if(link. Terminal. first == null){ link. Terminal. first = add. Link; link. Terminal. last = add. Link; }else{ link. Terminal. last. next = add. Link; link. Terminal. last = add. Link; } 102番地 2223 null 緑茶 null first last
検索(1) 商品種類が見つかる場合 public Item. Type search(int search. Id, Link. Terminal link. Terminal){ search. Id 2222 Link. Object current=link. Terminal. first; while(current!=null){ 100番地 102番地 if(current. data. id==search. Id){ System. out. println(“見つかりました”) ; 2221 2222 return current. data; } 紅茶 緑茶 current=current. next; } 102番地 104番地 System. out. println(“見つかりませんでした”); return null; } first current 104番地 2223 水 null last
検索(2) 商品種類が見つからない場合 public Item. Type search(int search. Id, Link. Terminal link. Terminal){ Link. Object current= link. Terminal. first; while(current!=null){ if(current. data. id==search. Id){ System. out. println(“見つかりました”) ; return current. data; } current=current. next; } System. out. println(“見つかりませんでした”); return null; } search. Id 3333 100番地 102番地 2221 紅茶 緑茶 102番地 null first current last current
削除(1) リストの中央で見つかる場合 public Item. Type delete(int delete. Id, delete. Id 2222 Link. Terminal link. Terminal){ Link. Object current=link. Terminal. first; 100番地 102番地 Link. Object previous; while(current!=null){ 2221 2222 if(current. data. id==delete. Id){ previous. next=current. next; 紅茶 緑茶 return current. data; } previous=current; 102番地 104番地 null 104番地 current=current. next; } } current first current previous × 104番地 2223 水 null last
削除(2) リストの最初で見つかる場合 public Item. Type delete(int delete. Id, Link. Terminal link. Terminal){ Link. Object current=first; Link. Object previous; while(current!=null){ if(current. data. id==delete. Id){ if(currrent==first){ first=current. next; return current. data; } null } } previous delete. Id 2221 100番地 102番地 104番地 2221 2222 2223 紅茶 緑茶 水 102番地 104番地 null first current first last
削除(3) リストの最後で見つかる場合 public Item. Type delete(int delete. Id, Link. Terminal link. Terminal){ delete. Id 2223 Link. Object current= link. Terminal. first; Link. Object previous; 100番地 102番地 104番地 while(current!=null){ if(current. data. id==delete. Id){ 2221 2222 2223 if(corrent== link. Terminal. last){ last=previous; 紅茶 緑茶 水 last. next=null; return current. data; } 104番地 null 102番地 previous=current; current=current. next; } } current first last current last previous } ×
- Slides: 44