我想把一個整數的數字一個一個地添加到串列中,我該怎么做
例如 int x=1993; 結果=[1,9,9,3]
另一個例子 int y=32 結果=[3,2]
uj5u.com熱心網友回復:
嘗試這個:
int yourNumber = 1234;
final yourInts = yourNumber.toString().split('').map((element) => int.parse(element)).toList();
print(yourInts);
uj5u.com熱心網友回復:
使用模數進行最有效的實作:
int x = 1993;
List<int> xList = [];
while(x > 0) {
xList.add(x % 10);
x = x ~/ 10;
}
xList = xList.reversed.toList();
print(xList);
此方法采用數字的模 10 來獲取個位并將其添加到串列中。然后除以 10 以將數字向右移動。由于數字是按您想要的相反順序添加的,因此串列最后會反轉。
uj5u.com熱心網友回復:
試試這個簡單的方法:
int x = 123456
var data = x.toString().split('');
print(data);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/505208.html
