gpt4 book ai didi

java - 数组中的数组?

转载 作者:行者123 更新时间:2023-11-29 04:34:42 24 4
gpt4 key购买 nike

好的,当我在我的 Java 编码类(class)中慢慢学习时,我被要求使用此代码来计算由字符数组的方法生成的小写字母的实例。我可以按照代码直到它抛出类似的东西。 “计数 [chars[currentIndex] - 'a']++;”

所以是的,代码说它正在创建一个名为 countLetters 的公共(public)方法,它需要在主体的某处调用,并且需要一个字符数组参数来启动,明白了。它创建了一个名为 counts 的整数数组,它的大小为 26,(与字母表中小写字母的总数相同,知道了。)然后它启动了一个 for 函数,通常用于数组。创建一个名为 currentIndex 的变量,默认值为 0,当当前索引小于 chars 字符数组的大小时,执行下面的操作。然后它得到了 for 循环实际上在做什么。呃,它到底在做什么?它正在增加计数索引的大小?整个事情很奇怪,就像数组中的某个数组并减去小写字母'a'的数值?它正在以某种方式修改计数的 currentIndex。为什么减去'a'的数值是必要的? 26 在这里不够吗?

有人可以用完全外行的方式慢慢解释这是如何工作的吗?我充其量只是一个新手程序员,这些东西让我很困惑,所以请耐心等待,我不是棚子里最锋利的工具,正如 Smashmouth 歌曲所唱的那样。不开玩笑了,如果你能分解发生的事情,我将不胜感激。

//count the occurrences of each letter
public static int[] countLetters(char[] chars){
//declare and create an array of 26 int
int[] counts = new int[26];

//for each lowercase letter in the array, count it
for (int currentIndex = 0; currentIndex < chars.length; currentIndex++)
counts[chars[currentIndex] - 'a']++;

return counts;
}

最佳答案

好吧,所以这一行:

counts[chars[currentIndex] - 'a']++;

所有这些的缩写:

// Get the current character from the array:
char character = chars[currentIndex];

// Characters are just numbers. Check out e.g. an "ASCII table".
// So, let's start treating them like numbers and take 'a' from that.
int indexInCounts = character - 'a';

// Increase the count for that letter:
counts[indexInCounts] = counts[indexInCounts] + 1;

为什么是“a”?

数组从 0 开始 - 您在问题中提到了这一点,所以到目前为止您似乎已经掌握了这些技巧。因此,如果我们希望 counts[0] 表示输入中 a 的数量,那么我们需要将 'a' 设为 0。

  • 'a'-'a'为0。
  • 'b'-'a' 为 1。

等等

因此,从我们的输入字符中去掉 'a' 可以方便地为我们提供一个对数组索引非常有用的数字。

Letters 带走 letters 太奇怪了!

计算机实际上只处理数字。拉起an ASCII table ,您将获得一种简单的方法来查看字母如何映射到基础数字(在 ASCII 编码方案中):

  • 97:小写字母“a”
  • 98:小写'b'
  • 99:小写字母“c”..等等!

希望您能看到它的发展方向!

例如,

98 (b) - 97 (a) 为我们提供了索引 1。

尝试一下,但不要忘记那些括号!

如果你想试验一下,你可以把上面那行换掉,但不要忘记 for 循环的括号!

for(int a=...)
doSomething(); // Only this first line is looped
doSomethingElse(); // This happens *once*!

这称为隐式括号,也是为了方便起见。所以,这是完整的扩展版本:

for (int currentIndex = 0; currentIndex < chars.length; currentIndex++)
{
// Everything in those brackets will be repeated.

// Get the current character from the array:
char character = chars[currentIndex];

// Characters are just numbers. Check out e.g. an "ASCII table".
// So, let's start treating them like numbers and take 'a' from that.
int indexInCounts = character - 'a';

// Increase the count for that letter:
counts[indexInCounts] = counts[indexInCounts] + 1;

}

我应该马上写这样的东西吗?

不是真的,不是!实际上,您通常会从上面看到的扩展版本开始(尽管大多数人会立即使用 counts[indexInCounts]++; ).当一个变量只使用一次时,通常更容易将实际值替换为 - 像这样,没有所有这些注释:

char character = chars[currentIndex];

int indexInCounts = character - 'a'; // Character is only used once.

counts[indexInCounts]++; // indexInCounts is only used once.

第 2 步:

char character = chars[currentIndex];
counts[character - 'a']++; // Character is only used once.

最后,神奇的线又回来了:

counts[chars[currentIndex] - 'a']++;

预测错误

如果您认为自己掌握了窍门,那么请尝试预测如果您在输入中插入一个邪恶的空格字符会出现什么错误。

剧透一下:

你会得到一个index out of range 异常。该 ASCII 表上的空间是 3232 - 97 是一个负数,远远超出了 counts 数组可接受的 0-25 范围!

附言我不是棚子里最锋利的工具 - 我将永远并且总是不同意(除了这首歌;那很棒):) 每个人都必须从某个地方开始,而你正在试一试,祝您一切顺利!

关于java - 数组中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42149905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com