DEV Community

wei chang
wei chang

Posted on

Summary and Sharing of Data Types in the Cangjie development Language for HarmonyOS Next

Hello everyone. Today, let’s summarize the data types in Cangjie.
Digital
The number types in Cangjie are complex and diverse. Firstly, they are divided into integer and floating-point types, namely Int and Float types. Int types include several types such as Int, Int8, Int32, and Int64. Float types also have several types such as Float16, Float32, and Float64. The following introduces the differences between them to everyone.
In fact, whether it is Int8, Int32 or Int64, they all belong to integer types; the only difference lies in their lengths.
For example, Int8 is the shortest, occupying only one byte;
Int16 occupies 2 bytes and is equivalent to short;
Int32 occupies 4 bytes and it is equivalent to Int.
Int64 occupies 8 bytes and is equivalent to long
String
The String type of Cangjie is similar to that of other languages, all being string. So far, I haven’t found anything to pay attention to.
Array
There are many types of arrays in Cangjie. The most basic one is the Array type. Array is used to define a relatively fixed array. It does not have addition or deletion operations, but only simple sorting, truncation, and query operations.

let arrayList1 = Array<Int64>([1, 2, 3, 4, 5, 6])
//截取
arrayList1.slice(0, 1)
//倒序
arrayList1.reverse()
//查询
arrayList1.indexOf(1)
Enter fullscreen mode Exit fullscreen mode

Next comes the ArrayList type, which adds operations such as adding, inserting and deleting on the basis of Array:

let arrayList2 = ArrayList<Int64>([1, 2, 3, 4, 5, 6])
//在头部添加
arrayList2.prepend(0)
//在尾部添加
arrayList2.append(7)
//在指定位置添加
arrayList2.insert(2, 0)
//删除元素
arrayList2.remove(1)
Enter fullscreen mode Exit fullscreen mode

Finally, there is the ObservedArrayList type, and correspondingly, there is also the ObservedArray type. They are typically used for state management. When the content of the array changes, they trigger the UI to update.
HashMap
A HashMap is an unordered sequence used to store key-value types. The type of each key-value pair is fixed, and the keys cannot be repeated:

let map = HashMap<String, String>([('姓名','幽蓝'),('职业','码农')])
//修改
map['姓名'] = '123'
//删除
map.remove('职业')
//取值
map.get('姓名')
//清空
map.clear()
Enter fullscreen mode Exit fullscreen mode

That’s all for today’s content. Thank you for reading.

Top comments (0)