我有一个“算法”,它的输入参数为整数k
。例如,我将k = 54321作为参数传递。我想用这种方法:
但是当涉及到for循环时,如果我通过char [position]获得项目,那么它会给我随机值。实际上,
public class Main {
public static void main(String[] args) {
System.out.println(sortMet(54321));
}
static ArrayList<Integer> sortMet(int k) {
ArrayList<Integer> tab_cyfr = new ArrayList<>();
char[] chars = String.valueOf(k).toCharArray();
int n = chars.length;
Arrays.sort(chars);
for (int i = 0; i < n; i++) {
tab_cyfr.add(chars[i]);
}
return tab_cyfr;
}
}
这应该返回[1,2,3,4,5],但返回[49,50,51,52,53],这些数字来自哪里?
对变量命名感到抱歉,我正在对局部变量进行随机练习。
答案 0 :(得分:3)
String.valueOf(k).toCharArray()
returns the characters in the String
as their char
representation. As per ASCII table 49
is the char value of 1
, 50
of 2
, and so on.
You should convert k
to digits, not to char
s. This can be done by using %
operation:
int k = 8421753;
ArrayList<Integer> digits = new ArrayList<>();
while (k > 0) {
digits.add(k % 10);
k /= 10;
}
Collections.sort(digits);
System.out.println(digits); // [1, 2, 3, 4, 5, 7, 8]
答案 1 :(得分:2)
It gets converted to a string, so the numbers are stored as ASCII values in the char array.
The problem is this line:
char[] chars = String.valueOf(k).toCharArray();
If you were to look up the ASCII values of the digits 1,2,3,4,5, you would see they are equal to 49, 50, 51, 52, 53,respectively.
To fix this issue, run a loop through the int number, take it apart digit by digit, and store them as ints
, not chars
, and then just sort your int[]
.
答案 2 :(得分:1)
Replacing your ArrayList<Integer>
to ArrayList<Character>
solve this problem because it store characters '1', '2', etc not their Integer values which are 49, 50...
So below code return what you expect: [1, 2, 3, 4, 5]
public static void main(String[] args) {
System.out.println(sortMet(54321));
}
static ArrayList<Character> sortMet(int k) {
ArrayList<Character> tab_cyfr = new ArrayList<>();
char[] chars = String.valueOf(k).toCharArray();
int n = chars.length;
Arrays.sort(chars);
for (int i = 0; i < n; i++) {
tab_cyfr.add(chars[i]);
}
return tab_cyfr;
}
答案 3 :(得分:0)
这些数字是数组中字符的ASCII码。例如,字符0
为48,字符1
为49,依此类推。
似乎您正在使用Integer
的数组列表,因此list元素将像标准int
和long
整数一样打印。要解决此问题,请将ArrayList<Integer>
替换为ArrayList<Character>
。
答案 4 :(得分:0)
此行无法编译:
tab_cyfr.add(chars[i]);
所以您得到[49,50,51,52,53]很奇怪。
即使编译,它也会添加chars[i]
的ascii值,
但是您想要它的数值。
一种获得它的方法是:
tab_cyfr.add(Integer.parseInt("" + chars[i]));
答案 5 :(得分:0)
toCharArray()
method returns the character array and when you sort array is sorted by its ASCII value. So those random numbers are ASCII values.
To convert char into Integer:-
static ArrayList<Integer> sortMet(int k) {
ArrayList<Integer> tab_cyfr = new ArrayList<>();
char[] chars = String.valueOf(k).toCharArray();
int n = chars.length;
for (int i = 0; i < n; i++) {
tab_cyfr.add(Character.getNumericValue(chars[i]));
}
Collections.sort(tab_cyfr);
return tab_cyfr;
}
答案 6 :(得分:0)
This code should work instead.
public static void main(String[] args) {
System.out.println(sortMet(54321));
}
static ArrayList<Integer> sortMet(int k) {
ArrayList<Integer> tab_cyfr = new ArrayList<>();
char[] chars = String.valueOf(k).toCharArray();
int n = chars.length;
Arrays.sort(chars);
for (int i = 0; i < n; i++) {
tab_cyfr.add(Character.getNumericValue(chars[i]));
}
return tab_cyfr;
}
Character.getNumericValue(chars[i]) ensures that you push the actual char value into tab_cyfr and not the ASCII value of it.