我討厭編碼已有的東西。我似乎無法在雅加達公共找到如下的方法將:j2se或雅加達公地區有什麼東西可以將數字轉換爲數字數組嗎?
long x= 12345;
int[] digits = toDigitsArray(x); //[1,2,3,4,5]
所以之前我的代碼我自己toDigitsArray()方法,沒有人知道是否存在無論是在J2SE或公共的相同呢?
我討厭編碼已有的東西。我似乎無法在雅加達公共找到如下的方法將:j2se或雅加達公地區有什麼東西可以將數字轉換爲數字數組嗎?
long x= 12345;
int[] digits = toDigitsArray(x); //[1,2,3,4,5]
所以之前我的代碼我自己toDigitsArray()方法,沒有人知道是否存在無論是在J2SE或公共的相同呢?
我想出了該方法的4個版本。
當與非常大的值測試和Long.MAX_VALUE所有四個給出正確的結果。
當使用Long.MIN_VALUE進行測試時,發現Math.abs(Long.MIN_VALUE)== Long.MIN_VALUE,所以您不能依靠它來擺脫減號!所以我必須擺脫它,所以我修改了版本。模數版本也給出Long.MIN_VALUE的錯誤值,這些數字是正確的,但它們都有負號。
現在的表現,我確實在那裏我有相同的輸入運行每個方法10 000 000次了一些測試,這個樣本的結果,但它是非常有代表性的人的:
輸入:-5507235864653430347
1:5110
2:7235
3:7564
4:25487
因此,迄今爲止獲勝者是方法1. 2和3不可靠,4非常緩慢。
public static int[] toIntArray(long x) {
int neg = (x<0)?1:0;
final char[] c = String.valueOf(x).toCharArray();
final int s = c.length-neg;
final int[] d = new int[s];
for (int i = 0; i < s; i++) {
d[i] = Character.getNumericValue(c[i+neg]);
}
if (neg==1) {
d[0] = d[0] * -1;
}
return d;
}
public static int[] toIntArray2(long x) {
int neg = (x<0)?1:0;
final int s = String.valueOf(x).length()-neg;
final int[] d = new int[s];
for(int i =s-1 ; i > -1; i--) {
d[i] = (int) (x%10);
x = x/10;
}
return d;
}
public static Object[] toIntArray3(long x) {
Deque<Integer> d = new ArrayDeque<Integer>(10);
if(x==0){
d.push(0);
return d.toArray();
}
while(x != 0) {
d.push((int) (x%10));
x = x/10;
}
return d.toArray();
}
public static int[] toIntArray4(long x) {
int neg = (x<0)?1:0;
final String[] c = String.valueOf(x).split("");
final int s = c.length-neg;
final int[] d = new int[s-1];
for (int i = 1; i < s; i++) {
d[i-1] = Integer.parseInt(c[i+neg]);
}
if (neg==1) {
d[0] = d[0] * -1;
}
return d;
}
還沒有見過。
「while(n!= 0){push(n%10); n = n/10}」應該足夠了。
如果你不想混淆列表,那麼做兩次。首先計算數組中的條目數,然後計算數組中的條目數。
比字符串解決方案好得多。 – 2009-07-31 17:15:37
不幸的是,如果long超過整數的範圍,mod運算符將無法工作。 – Yishai 2009-07-31 17:22:36
我認爲這可以起作用,或者至少帶你進一步。
long x = 11234;
String[] s = Long.toString(x).split("");
int[] r = new int[s.length - 1];
for (int i = 1; i < s.length; i++)
r[i - 1] = Integer.parseInt(s[i]);
只有當x> = 0時纔有效 – Yishai 2009-07-31 16:27:45
x.toString()。toCharArray()?結果將是一個chars數組,而不是一個整數數組,正如您在示例中查找的那樣,但您可以在使用它們時將這些chars轉換爲整數,或者取決於您要使用它的內容,字符可以很好。
就我所見,沒有這樣的功能存在。滾動你自己有一些值得討論的陷阱。
我通過一些參數運行了三個選項(分割一個字符串並進行模數運算並生成一個char數組),這是我所能看到的最「高效」的。注意一些假設:
首先模數路由,乍一看應該是最有效的。但是,如果數字確實很長(如超過int的最大值),那麼mod運算符會溢出並給出垃圾結果。然後我們要搬到BigInteger的(只要我可以告訴大家,如果有人看到一個更好的解決方案,請評論):
public static void main(String[] args) {
long x = 981212131233123L;
int[] r = new int[calculateSize(x)];
boolean positive = fillArrayWithDigits(x, r);
//r = [9, 8, 1, 2, 1 ...
}
private static boolean fillArrayWithDigits(long y, int[] r) {
boolean positive = y >= 0;
y = Math.abs(y);
BigInteger ten = BigInteger.valueOf(10);
for (int i = r.length; i > 0; i--) {
r[i-1] = BigInteger.valueOf(y).mod(ten).intValue();
y /= 10L;
}
return positive;
}
private static int calculateSize(long y) {
int size = 0;
do {
size++;
y /= 10L;
} while (y != 0);
return size;
}
在這個月底,你必須表明一個布爾值,如果數量爲正,以及包含數字的數組。
在做一個字符串解析而言,「最好的」我能想出是:
public static void main(String[] args) {
long x = 981212131233123L;
boolean positive = x >= 0;
x = Math.abs(x);
String[] s = Long.toString(x).split("");
int[] r = new int[s.length - 1];
for (int i = r.length; i > 0; i--) {
r[i-1] = Integer.parseInt(s[i]);
}
//r = [9, 8, 1, 2, 1 ...
}
的字符數組的方法是非常相似:
public static void main(String[] args) {
long x = 981212131233123L;
boolean positive = x >= 0;
x = Math.abs(x);
char[] s = Long.toString(x).toCharArray();
int[] r = new int[s.length];
for (int i = r.length - 1; i > -1; i--) {
r[i] = Character.digit(s[i], 10);
}
//r = [9, 8 ,1, 2, 1 ....
}
,但似乎最有效
首先,我不知道你想要做什麼任何現有的庫法,我想不出來搜索一個很好的方式。 (如果有人能解決這個問題,他們應該得到諾貝爾獎!!)
所有涉及創建/分割字符串或使用BigInteger的答案都是不必要的低效率。這裏是我的解決方案:
public int[] toDigitsArray(long num) {
if (num < 0) {
throw new IllegalArgumentException("negative num");
} else if (num == 0) {
return new int[]{0}; // simplifies the rest of the code
}
int[] tmp = new int[20]; // big enough to hold all digits of Long.MAX_VALUE
int i = tmp.length - 1;
while (num != 0) {
tmp[i--] = num % 10;
num = num/10;
}
int[] res = new int[tmp.length - i];
System.arraycopy(tmp, i + 1, res.length, res, 0);
return res;
}
(我還沒有編譯/測試這...但專注於算法)。
不,很肯定你會寫這個。 – 2009-07-31 16:11:21