Re: [請益] 沒有key的陣列是如何去利用?

看板PHP作者 (LaPass)時間13年前 (2012/07/17 15:29), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《coldollsheep (加油加油^^)》之銘言: : 請問一個陣列的問題 : 因為PHP的陣列的key可以自行決定 : 不一定要使用0 1 ..... : 這真的很方便 : 就算在javascript裡面也可以用物件來達到相似的功能 : --- : 想藉這個版問一個不是PHP的問題 : A這個類別裡面有A1 A2兩筆 : B這個類別裡面有B1 B2 B3 三筆 : 在PHP裡面很簡單 : $ary['A'][] = 'A1'; (...以此類推) : 如果在C# 或是java的世界裡面是怎麼做的? : 其實不限定何種語言 : 我只是想知道不是PHP的語言是怎麼做的 大多數的物件導向的語言,都有容器物件 幾乎都可以找到類似的東西..... C# Dictionary Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add("cat", 2); dictionary.Add("dog", 1); if (dictionary.ContainsKey("cat")) int value = dictionary["cat"]; 或者,自己寫一個索引子的物件 C# 索引子 class SampleCollection<T> { private T[] arr = new T[100]; public T this[int i] //<= 索引可指定任何類型 { get{return arr[i];} set{arr[i] = value;} } } 使用時: SampleCollection<string> stringCollection = new SampleCollection<string>(); stringCollection[0] = "Hello, World"; string stringCollection[0]; Java Map Map<String,Integer> map = new HashMap<String,Integer>(); map.put("Apple",1); int i= map.get("Apple"); 還有,PHP的陣列,其實比較像是「看起來像陣列的容器物件」吧? 他在從key找value時候,好像是用hash的方式去找 -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.59.16.65

07/17 18:34, , 1F
是的,All arrays in php are hashtables. Check
07/17 18:34, 1F

07/17 18:34, , 2F
07/17 18:34, 2F
文章代碼(AID): #1G1HHc_U (PHP)
文章代碼(AID): #1G1HHc_U (PHP)