前段时间将一个项目代码从java转为鸿蒙,总结了其中java转ArkTS语法的一些经验
前段时间将一个项目代码从java转为鸿蒙,总结了其中java转ArkTS语法的一些经验。整个项目代码花了10多天时间,转完后运行bug还是较少的。
以下是记录下来的一些,部分未经常出现的可能没有记录
//向下取整
(int) -> Math.floor()
Integer.parseInt("10") -> parseInt("10")
//字符串
str.contains("=") -> str.includes('=')
str.compareTo() -> str.localeCompare()
y.charAt(i) - '0' -> y.charCodeAt(i) - '0'.charCodeAt(0)
String.valueOf() -> String()
//数组
l.addAll(fs) -> l.concat(fs), liuNianArr.push(...liuNian);
List
list.add("123") -> list.push("123")
//空数据判断
list.isEmpty() -> list.length === 0
//在数组的开头插入一个元素
l.add(0, "s"); -> l.unshift("s");
list.remove(i); -> list.splice(i, 1);
//Set
Set
arr = new ArrayList<>(set); -> this.arr = Array.from(set);
//map
map.containsKey("key1") -> map.has("key1")
//循环
for (m: months) -> for (const m of this.months)
//空值判断
if (year == null || year ==undefined)