c 集合小記

2021-04-02 22:26:37 字數 2648 閱讀 7650

正在揣摩《asp.net入門經典-c#程式設計篇》,這書看起來還行,look到集合了!需要摘些**!自己沒事看看了!--嗨!就是作個筆記!開始。。

class1

using system;

using system.collections;

namespace yjtestcon

;console.writeline("**************",a.tostring());

//陣列的新增

cow mycow1=new cow("deirder");

animalarray[0]=mycow1;

animalarray[1]=new chicken("ken");

//陣列還可以重寫

//animalarray[0]=new cow*("alma");

foreach(animal myanimal in animalarray)

object added to array collection,"+

"name=",myanimal.tostring(),myanimal.name);

}console.writeline("array collection containsobjects.",

animalarray.length);//輸出陣列的專案個數

animalarray[0].feed();//陣列是強型別化的, 可以直接訪問自己所包含的專案型別,可以直接呼叫專案的方法

((chicken)animalarray[1]).layegg();

//由於陣列的型別是抽象類animal,因而不能訪問派生類所提供的方法.而必須使用資料型別轉換!

console.writeline();

console.writeline("create an arraylist type collection of animal"+

"objects and use it:");

//animalarraylist是個arraylist集合

arraylist animalarraylist=new arraylist();

//animalarraylist集合的新增

cow mycow2=new cow("hayley");

animalarraylist.add(mycow2);

animalarraylist.add(new chicken("roy"));

foreach(animal myanimal in animalarraylist)

object added to arraylist collection,"+

"name=",myanimal.tostring(),myanimal.name);

}console.writeline("arraylist collection containsobjects.",

animalarraylist.count);//輸出arraylist集合的專案個數  用到了  arraylist.count

//arraylist集合是system。object物件的集合(通過多型性賦給animal物件),所以必須用型別轉換!

((animal)animalarraylist[0]).feed();

((chicken)animalarraylist[1]).layegg();

console.writeline();

console.writeline("additional manipulation of arraylist:");

//刪除mycow2專案 也可以用:animalarraylist.remove(mycow2)

animalarraylist.removeat(0);

//由於mycow2被刪除了,集合裡只有chicken物件,所以可以用下面的語句訪問它

((animal)animalarraylist[0]).feed();//由於只有mycow2被刪除所以arraylist

//集合可以用addrenge()方法一次新增好幾個專案.這個方法接受帶有icollection介面的任何物件,包括前面**建立的animalarray陣列

//這個方法專用arraylist類

animalarraylist.addrange(animalarray);//這樣就新增了兩個專案(陣列有兩個物件)

//可以訪問集合中的第三個物件了◎

((chicken)animalarraylist[2]).layegg();

foreach(animal myanimal in animalarraylist)

object added to arraylist collection,"+

"name=",myanimal.tostring(),myanimal.name);

}console.writeline("the animal coalled is at index .",

mycow1.name,animalarraylist.indexof(mycow1));

mycow1.name="janice";

console.writeline("the animal is now called .",

((animal)animalarraylist[1]).name);

console.read();}}

}

java 集合小記1

集合集合顧名思義,就是相同型別的東西湊一塊了。collection 生出了幾個兒子 list set map queue.和他這幾個兒子交流了幾天交流的我是混混燉燉,感覺自己被玩壞了,看似很簡單,越看越不是那麼回事。1.這幾個兒子派生了孫子,平常都是孫子幹活。先說一下 大兒子 list list之下...

C 函式小記

1.函式預設引數 a 非預設引數不能放在預設引數右邊 b 函式宣告時使用預設引數,定義照常?void add int m,int n 0 void add int m 0,int n 錯誤,非預設引數不能放在預設引數右邊 void add int m,int n 2.函式過載 關鍵 函式的引數列表,...

C 型別小記

最近在專案中遇到一個很有意思的問題。簡單重現一下,大概是如下情況。在檔案f1.cpp中定義字串 char str 16 abcdefg 在其他檔案中,比如f2.cpp中使用extern進行一下宣告 extern char str 在對str進行操作時,比如最簡單的 void func 這時候竟然出現...

C 做題小記

include intmain void 儲存輸入資料用的陣列 int i 0 記錄輸入的字元個數方便輸出 char z 0 解決方法就是加入臨時變數來先儲存再判斷 while 1 for int k 0 k i k printf c c k 把儲存的資料輸出 printf n return0 精簡...

C 集合 集合

1,什麼是集合 集合 collection 類是專門用於資料儲存和檢索的類。這些類提供了對棧 stack 佇列 queue 列表 list 和雜湊表 hash table 的支援。大多數集合類實現了相同的介面。集合 collection 類服務於不同的目的,如為元素動態分配記憶體,基於索引訪問列表項...