Wenn Du sicher sein kannst, dass die Eingangswerte immer positiv sind und der aneinandergehängte Wert mit int darstellbar ist, dann ist diese Implementation meines Erachtens schöner:
Code:
/**
* Concatenates all integer values in the given <code>int[]</code> array to
* one integer, assuming all elements are <code>& 0</code> and the
* concatenated value is <code>& Integer.MAX_VALUE</code>. In all other
* cases the result is wrong.
*
* @param array the integer elements to concatenate
* @return the concatenated integer value
*/
private static int concatenateToInteger(int[] array) {
int result = 0;
for (int i = 0; i < array.length; i++) {
int value = array[i];
int multiplyWith;
for (multiplyWith = 10; value >= multiplyWith;) {
multiplyWith *= 10;
}
result = result * multiplyWith + value;
}
return result;
}
tja man könnte es schon ausrechnen... die frage ist halt, ob man alle ausnahmen und sonderfälle berücksichtigt hat... eher nicht
die zweite frage ist, was macht Integer.parseInt? vermutlich auch ausrechnen...
Code:
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}
int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;
if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else {
limit = -Integer.MAX_VALUE;
}
multmin = limit / radix;
if (i < max) {
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}
radix ist 10...
dritte frage: berücksichtigt diese methode mehr sonderfälle und ausnahmen als deine? vermutlich
vierte frage: würde diese methode besser getest? auf jeden fall!
ich würde auf Integer.parseInt setzen ;-)
Auf jeden fall macht es Sinn, sich mit dem Problem auseinander zu setzen und es sicht sicher nicht schlecht, wenn man sich überlegt ob mans selber machen kann.
Oft ist es aber in der Wirtschaft sinnvoller, vorgefertigte erprobte Methoden zu verwenden.
Wenn Du sicher sein kannst, dass die Eingangswerte immer positiv sind und der aneinandergehängte Wert mit int darstellbar ist, dann ist diese Implementation meines Erachtens schöner:
Code:
/**
* Concatenates all integer values in the given <code>int[]</code> array to
* one integer, assuming all elements are <code>& 0</code> and the
* concatenated value is <code>& Integer.MAX_VALUE</code>. In all other
* cases the result is wrong.
*
* @param array the integer elements to concatenate
* @return the concatenated integer value
*/
private static int concatenateToInteger(int[] array) {
int result = 0;
for (int i = 0; i < array.length; i++) {
int value = array[i];
int multiplyWith;
for (multiplyWith = 10; value >= multiplyWith;) {
multiplyWith *= 10;
}
result = result * multiplyWith + value;
}
return result;
}
Ging mir nur darum, dass ich normaler Weise nicht Strings aufbaue um Zahlen zu erhalten. Weil's mir einfach nicht so gut gefällt. Einschränkungen hab ich ja oben geschrieben. Ansonsten war's nur Sport.
Du möchtest also die Zahl 1004532132100 als Integer darstellen? Schonmal auf die Idee gekommen, dass die Zahl etwas größer ist als Integer.MAX_VALUE (2147483647)?
""unsigned int" in java?" Hab ich mich auch gefragt =)
Man könnte aber z.B. eine IllegalArgument Exception werfen, falls man > Int.MAX oder < 0 geht, fals jemand zu faul/dumm war (so wie ich) die JavaDoc zu lesen =)
Du wirst lachen, ich hatte die Version mit IllegalArgumentException schon fertig und hab sie dann wieder rausgeschmissen, weil ich davon ausging, dass der TO sicher nicht so komplizierte Sachen machen wollte.