java - Converting string to character array to int array to linkedlist -
if input string "-32323" out set [-50,-51,-50,-51,-50], , not sure these values coming from. output trying [-3,-2,-3,-2,-3]
static linkedlist<integer> list = new linkedlist<integer>(); public static linkedlist<integer> method(string s) { char[] listofchar; int[] num; list = new linkedlist<integer>(); listofchar = s.tochararray(); if (listofchar[0] == '-') { num = new int[listofchar.length - 1]; (int = 0; < num.length; i++) { num[i] = -1 * listofchar[i + 1]; } } else { num = new int[listofchar.length]; (int = 0; < num.length; i++) { num[i] = listofchar[i]; } } (integer x : num) { list.push(x); } homecoming list; }
problem:
listofchar[i + 1]; it convert char decimal value of (ex. char 3 equals 51).
click here know decimal values of different characters
solution:
num[i] = -1 * (listofchar[i + 1] - '0'); you need deduct char 0 create 3, because char 0 in decimal 48 means (51 - 48) equals 3.
java
No comments:
Post a Comment