我是java
的新手,也是编程。我已经给出了一个计算唯一字符出现次数的作业。我只能用
阵列。我做了流动的代码 -
public class InsertChar{
public static void main(String[] args){
int[] charArray = new int[1000];
char[] testCharArray = {'a', 'b', 'c', 'x', 'a', 'd', 'c', 'x', 'a', 'd', 'a'};
for(int each : testCharArray){
if(charArray[each]==0){
charArray[each]=1;
}else{
++charArray[each];
}
}
for(int i=0; i<1000; i++){
if(charArray[i]!=0){
System.out.println( i +": "+ charArray[i]);
}
}
}
}
对于testCharArray
,输出应为 -
a: 4
b: 1
c: 2
d: 2
x: 2
但它给了我以下的outpu -
97: 4
98: 1
99: 2
100: 2
120: 2
我该如何解决这个问题?
答案 0 :(得分:3)
2015/05/31 00:41:23 INFO - jmeter.engine.StandardJMeterEngine: Running the test!
2015/05/31 00:41:23 INFO - jmeter.samplers.SampleEvent: List of sample_variables: []
2015/05/31 00:41:23 INFO - jmeter.gui.util.JMeterMenuBar: setRunning(true,*local*)
2015/05/31 00:41:23 INFO - jmeter.engine.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2015/05/31 00:41:23 INFO - jmeter.engine.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2015/05/31 00:41:23 INFO - jmeter.engine.StandardJMeterEngine: Thread will continue on error
2015/05/31 00:41:23 INFO - jmeter.threads.ThreadGroup: Starting thread group number 1 threads 1 ramp-up 0 perThread 0.0 delayedStart=false
2015/05/31 00:41:23 INFO - jmeter.threads.ThreadGroup: Started thread group number 1
2015/05/31 00:41:23 INFO - jmeter.engine.StandardJMeterEngine: All thread groups have been started
2015/05/31 00:41:23 INFO - jmeter.threads.JMeterThread: Thread started: Thread Group 1-1
2015/05/31 00:41:23 INFO - jmeter.threads.JMeterThread: Thread is done: Thread Group 1-1
2015/05/31 00:41:23 INFO - jmeter.threads.JMeterThread: Thread finished: Thread Group 1-1
2015/05/31 00:41:23 INFO - jmeter.engine.StandardJMeterEngine: Notifying test listeners of end of test
2015/05/31 00:41:23 INFO - jmeter.gui.util.JMeterMenuBar: setRunning(false,*local*)
为i
,因此您要打印每个int
的整数值。您必须将其强制转换为char
才能看到字符。
变化
char
到
System.out.println( i +": "+ charArray[i]);
答案 1 :(得分:3)
您正在将索引打印为System.out.println( (char)i +": "+ charArray[i]);
。在打印之前尝试将其投射到int
:
char
答案 2 :(得分:2)
您的程序正在打印ascii
字符表示。您只需将acsii
数字转换为字符。
public class InsertChar{
public static void main(String[] args){
int[] charArray = new int[1000];
char[] testCharArray = {'a', 'b', 'c', 'x', 'a', 'd', 'c', 'x', 'a', 'd', 'a'};
for(int each : testCharArray){
if(charArray[each]==0){
charArray[each]=1;
}else{
++charArray[each];
}
}
for(int i=0; i<1000; i++){
if(charArray[i]!=0){
System.out.println( **(char)**i +": "+ charArray[i]);
}
}
}
}
这应该有用。
进一步阅读:
How to convert ASCII code (0-255) to a String of the associated character?
答案 3 :(得分:2)
您正在打印每个stop
的{{1}} [NSArray arrayWithObjects: ... , nil];
个代表。您必须通过强制转换将其转换为[NSNumber numberWith...];
-
#pragma mark
答案 4 :(得分:1)
您的 C:\Education\DataStructure_Algorithms\List\List.cpp|252|
error: need 'typename' before 'std::unordered_set<Node<T>*>::
const_iterator' because 'std::unordered_set<Node<T>*>' is a dependent scope|
是一个int数组。你已经存储了char。所以你必须将字符转换为int。
您必须在最后一个循环中进行更改 -
testCharArray
此转换使int成为char。每个字符都有一个int值。例如 -
'a'有97,因为它是for(int i=0; i<1000; i++){
if(charArray[i]!=0){
System.out.println( (char)i +": "+ charArray[i]); //cast int to char.
}
}
值
'b'有98,因为它是int
值