嗨,能否解释一下这段代码的工作原理? [英] Hi can you please explain working of this code ?

查看:86
本文介绍了嗨,能否解释一下这段代码的工作原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package arrays;

public class Chararray {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s ="Hello";
		int letter[] = new int[256];
                for(char c:s.toCharArray()){
			letter[c]++;
			System.out.println(c);
			System.out.println(letter[c]);
		}

	}

}

<pre lang="java">





我的尝试:



我的输出是



What I have tried:

My output is

H
1
e
1
l
1
l
2
o
1



但我能够了解循环如何工作


But am able to follow how does the loop works

for(char c:s.toCharArray()){
			letter[c]++;

推荐答案

for循环很简单,它只是一个增强的for循环:在Java中寻找 - GeeksforGeeks [ ^ ]它对输入字符串中的每个字符执行一次。

后面的行计算原始字符串中每个字母的数量:

The for loop is pretty simple, it's just an "enhanced for loop": Loops in Java - GeeksforGeeks[^] It executes once for each character in the input string.
The line after counts how many of each letter are in the original string:
letter[c]++;

每个字母用作letters数组的索引,该整数的内容为递增。

然后你打印这封信,然后目前的数量。如果我旋转90度则更清楚:

Each letter is used as an index to the "letters" array, and the content of that integer is incremented.
You then print the letter, and the current count. If I "rotate it 90 degrees" it's clearer:

Hello
11121

如果您仍然不确定,请使用调试器并观察执行过程中发生的情况!

If you still aren't sure, use the debugger and watch what happens as it is executing!


以下是该循环和代码的工作方式:





Here is how that loop and code is working:

the line
for(char c:s.toCharArray())



是一个增强的for循环,表示:逐个遍历s字符串中的每个字符。您需要将String转换为char数组,因为您不能使用增强的for循环来遍历String - 您将收到编译错误(请参阅这里有更多细节)。



当代码循环遍历每个字符时,它设置的值是


is an enhanced for loop that says: go through every character in the s string one by one. You need to convert the String to a char array because you can't use an enhanced for loop to loop through a String - you will get a compilation error (see here for more details).

As the code is looping over each character, it's setting the value in the

letter

int array。创建数组时,它的大小为256,所有值都初始化为0.



当它到达Hello中的第一个字符时,H ,代码将索引72的int数组设置为1.为什么72?因为这是ASCII值H - 请参阅此ASCII表以查看不同字符的数值



行:



int array. When the array is created, it's 256 in size and all the values are initialized to 0.

When it gets to the first character in "Hello", "H", the code sets the int array at index 72 to 1. Why 72? Because that's the ASCII value of "H" - see this ASCII table to see numeric values for different characters.

The line:

letter[c]++;





将该数组索引处的值递增1.这意味着如果使用相同的字母,它将增加int值再次在同一指数。这就是为什么在读取Hello中的第二个l后,您会看到值为2。在字符l的情况下,该字母的ASCII值为108.这意味着





increments the value at that array index by 1. That means if the same letter is used, it will increment the int value at the same index again. That is why after it reads the second "l" in "Hello" you see a value of "2". In the case of the character "l", that letter has an ASCII value of 108. So that means

letters[108]





增加两倍,这就是它显示2的原因。


您甚至可以在最后添加以下代码以确认使用的数组索引:





was incremented twice and that's why it displays 2.

You can even add the following code at the end to confirm the array indices that were used:

System.out.println(letter[72]);  //H: 1
System.out.println(letter[101]); //e: 1
System.out.println(letter[108]); //l: 2
System.out.println(letter[111]); //o: 1





数组中的所有其他索引都是0(默认值)。 />


来源: Java循环 - 终极指南


引用:

您能解释一下这段代码的工作原理吗?

Hi can you please explain working of this code ?



您不理解此代码,学会使用调试器,您将看到此代码的执行方式。



您的代码行为不符合您的预期,并且你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的应该是什么做,它没有发现错误,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

http://docs.oracle.com/javase/7/docs /technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里只显示你的内容你的代码正在做,你的任务是与它应该做的事情进行比较。


You don't understand this code, learn to use the debugger and you will watch how this code execute.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于嗨,能否解释一下这段代码的工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆