[英]Is it correct to convert 2D CharArray to String and use .charAt() to compare a character?
So I have a char
variable called "temp" which I'd like to compare to the element stored in "X" CharArray[X][Y]
while I'm in a third for
loop after the 2D array. 因此,我有一个名为“ temp”的char
变量,我想将其与“ X”中存储的元素进行比较CharArray[X][Y]
而我在二维数组之后的第三个for
循环中。
For example: 例如:
char temp;
temp = ' ';
String end;
end = "";
for (int i = 0; i < CharArray.length; i++){
for (int m = 0; m < 2; m++){
if (somethingY){
if (somethingZ){
for (int j = 0; j < something.length; j++){
//something
temp = somethingX;
if (temp == String.valueOf(CharArray[i][m]).charAt(0)){
end = String.valueOf(CharArray[i][m]);
System.out.print(end);
}
}
}
}
}
}
I've tried printing "temp" where it says "temp = somethingX" and it prints just fine. 我试过打印“ temp”,其中显示“ temp = somethingX”,并且打印得很好。 But when I try to save the String
into a String variable
, it will not print the variable called "end". 但是,当我尝试将String
保存到String variable
,它将不会打印名为“ end”的变量。
According to this , it won't do anything if the object is something else, but "end" is a String
. 根据此 ,如果对象是别的东西它不会做任何事,但“终端” 是一个String
。
So, what am I doing wrong? 那么,我在做什么错呢?
EDIT: In case there's a confusion, "I'm trying to print
"end", but I figured if temp == String.valueOf(CharArray[i][m]).charAt(0)
is correct, so should "end"'s part.". 编辑:以防万一,“我正在尝试print
”结束“,但我想如果temp == String.valueOf(CharArray[i][m]).charAt(0)
是正确的,那么”结束“ “的一部分。”
EDIT2: Defined "temp" for people... EDIT2:为人定义了“温度” ...
EDIT3: I tried " end.equals(String.valueOf(CharArray[i][m]));
", but still nothing happens when I try to print it. EDIT3:我尝试了“ end.equals(String.valueOf(CharArray[i][m]));
”,但是当我尝试打印它时仍然没有任何反应。 I get no errors nor anything. 我没有任何错误。
EDIT4: I tried putting String.valueOf(CharArray[i][m]).charAt(0)
into a another variable called "temp2" and doing if (temp == temp2)
, but still the same thing. EDIT4:我尝试将String.valueOf(CharArray[i][m]).charAt(0)
放入另一个名为“ temp2”的变量中,并进行if (temp == temp2)
,但还是一样。
EDIT5: I tried temp == CharArray[0][m]
and then end = CharArray[0][m]
, but still nothing prints. EDIT5:我尝试了temp == CharArray[0][m]
CharArray temp == CharArray[0][m]
,然后end = CharArray[0][m]
,但仍然没有任何输出。
EDIT6: OK. EDIT6:好的。 Sense this will never get resolved, I'll just say the whole point of my problem. 感觉这将永远无法解决,我只想说出我的问题的全部要点。 -> I have an ArrayList
where each line is a combination of a letter, space and a number (eg "E 3"). ->我有一个ArrayList
,其中每行是字母,空格和数字(例如“ E 3”)的组合。 I need to check if a letter is repeating and if it is, I need to sum the numbers from all repeating letters. 我需要检查一个字母是否重复,如果需要,我需要对所有重复字母的数字求和。
For example, if I have the following ArrayList
: 例如,如果我有以下ArrayList
:
Z 3
O 9
I 1
J 7
Z 7
K 2
O 2
I 8
K 8
J 1
I need the output to be: 我需要的输出是:
Z 10
O 11
I 9
J 8
K 10
I didn't want people to do the whole thing for me, but it seems I've no choice, since I've wasted 2 days on this problem and I'm running out of time. 我不希望人们为我做全部事情,但是似乎我别无选择,因为我在这个问题上浪费了2天,而且时间已经用完了。
Use a map : 使用地图:
ArrayList<String> input=new ArrayList<String>();
input.add("O 2");
input.add("O 2");
Map<String, Integer> map= new HashMap<String, Integer>();
for (String s:input) {
String[] splitted=s.split(" ");
String letter=splitted[0];
Integer number=Integer.parseInt(splitted[1]);
Integer num=map.get(letter);
if (num==null) {
map.put(letter,number);
}
else {
map.put(letter,number+num);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + Integer.toString(entry.getValue()));
}
Without using a map : 不使用地图:
ArrayList<String> input=new ArrayList<String>();
input.add("O 2");
input.add("O 2");
ArrayList<String> letters=new ArrayList<String>();
ArrayList<Integer> numbers=new ArrayList<Integer>();
for (String s:input) {
String[] splitted=s.split(" ");
String letter=splitted[0];
Integer number=Integer.parseInt(splitted[1]);
int index=-1;
boolean isthere=false;
for (String l:letters) {
index++;
if (l.equals(letter)) {
isthere=true; //BUGFIX
break;
}
}
if (isthere==false) { //BUGFIX
letters.add(letter);
numbers.add(number);
}
else {
numbers.set(index,numbers.get(index)+number);
}
}
for (int i=0; i < letters.size(); i++) {
System.out.println(letters.get(i));
System.out.print(numbers.get(i));
}
Converting it back to have a nice output : 将其转换回一个不错的输出:
ArrayList<String> output=new ArrayList<String>();
for (int i=0; i < letters.size(); i++) {
output.add(letters.get(i)+" "+Integer.toString(numbers.get(i));
}
Feel free to comment if you are having any questions. 如有任何疑问,请随时发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.