在大多数编程语言中,可以使用以下方式在一行中初始化集合:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_dict = {'key1': 'value1', 'key2': 'value2'}
my_set = {1, 2, 3}
int[] myArray = {1, 2, 3};
ArrayList<Integer> myList = new ArrayList<>(Arrays.asList(1, 2, 3));
HashMap<String, String> myMap = new HashMap<String, String>() {{ put("key1", "value1"); put("key2", "value2"); }};
HashSet<Integer> mySet = new HashSet<>(Arrays.asList(1, 2, 3));
let myArray = [1, 2, 3];
let myObject = { key1: 'value1', key2: 'value2' };
let mySet = new Set([1, 2, 3]);
let myMap = new Map([['key1', 'value1'], ['key2', 'value2']]);
int[] myArray = new int[] {1, 2, 3};
List<int> myList = new List<int> {1, 2, 3};
Dictionary<string, string> myDict = new Dictionary<string, string> {{"key1", "value1"}, {"key2", "value2"}};
HashSet<int> mySet = new HashSet<int> {1, 2, 3};
$myArray = [1, 2, 3];
$myAssocArray = ['key1' => 'value1', 'key2' => 'value2'];
$mySet = new \Ds\Set([1, 2, 3]);
$myMap = new \Ds\Map([['key1', 'value1'], ['key2', 'value2']]);
以上仅为常见编程语言的示例,不同编程语言的初始化集合方式可能会有所不同。在实际开发中,根据所使用的编程语言选择相应的语法来初始化集合。
领取专属 10元无门槛券
手把手带您无忧上云