尝试理解整数数组内的charAt方法

huangapple go评论87阅读模式
英文:

Trying to understand charAt method inside an integer array

问题

我正在尝试理解,如何在整数数组内使用字符串元素。
我正在解决一个与数组有关的问题,我试图在一个字符串中存储字符的频率。

int[] letters = new int[128];
for(int i = 0; i < s.length(); i++){
    System.out.println(s.charAt(i));
    letters[s.charAt(i)] =  letters[s.charAt(i)] + 1;    
}

我的问题是,由于letters是一个整数数组,s.charAt(i)会返回一个字符串字符。
我正在打印出letter[s.charAt(i)],假设是letters['a'],它确实打印出一个数字。但是通过使用charAt方法,它不是索引。我们如何将字符作为索引访问?

英文:

I am trying to understand, how can we use a string element inside the integer array.
I am solving one of the array related question where I am trying to store frequency of characters in a string.

int[] letters = new int[128];
for(int i = 0; i &lt; s.length(); i++){
    System.out.println(s.charAt(i));
    letters[s.charAt(i)] =  letters[s.charAt(i)] + 1;    
}

My question is, since letters is an integer array, s.charAt(i) will return a string character.
I am printing out, letter[s.charAt(i)] which lets say is letters['a'] which does print out a number. But by using charAt its not. How can we access the char as an index?

答案1

得分: 1

Sure, here's the translated content:

我正在努力理解,如何在整数数组内部使用字符串元素。

这取决于您在"use"中的具体意思。如果您想将字符串值存储到整数数组中 - 您是不能的。无论是int[]还是Integer[],都不允许您在其中存储任何字符串元素。您将得到一个编译时错误,类似于:

java: 不兼容的类型:无法将java.lang.String转换为int

我正在打印出,letter[s.charAt(i)],假设这是letters['a']

String#charAt(int)返回一个单个字符,您尝试的是使用字符索引访问您的letters数组的位置,这将导致编译过程停止,并显示以下消息:

java: 找不到符号
    符号:变量yourCharacter

我们如何访问字符作为索引?

您无法这样做。任何数组的索引始终是正整数。

英文:

>I am trying to understand, how can we use a string element inside the integer array.

It depends on what you mean in use. If you want to store a string value into integer array - you cannot. Neither int[] or Ingeter[] will allow you to store any string element in them. You will get a compile-time error, something like:

java: incompatible types: java.lang.String cannot be converted to int

>I am printing out, letter[s.charAt(i)] which lets say is letters['a']

String#charAt(int) returns a single character, and what you're trying to do, is to access your letters array's slot with a character index, which, in turn, would also stop your compilation process with the message:

java: cannot find symbol
    symbol:   variable yourCharacter

>How can we access the char as an index?

You cannot. Index of any array is always a positive integer number.

答案2

得分: 0

让我们假设您的字符串是

String str = "stack";
for (int i = 0; i < str.length(); i++) {
    System.out.println(str.charAt(i));
}

// charAt 方法的签名是 charAt(int index)。在这里,它接收字符串的索引(由 i 表示),并打印字符。您的假设有点错误。您可以参考官方 Java 文档中的 String 类,以更好地理解方法的参数、功能和返回值。
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

英文:

Let us say your string is

String str = &quot;stack&quot;
for(int i=0; i&lt;str.length(); i++) {
System.out.println(str.charAt(i))
}

// Signature of charAt method is, charAt(int index). Here, it takes the index of the string as referred by i and prints the character. Your assumption is slightly wrong. You can refer to String class of the official Java documentation to get the better understanding of the methods arguments, what it does and what it returns.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

huangapple
  • 本文由 发表于 2020年9月11日 11:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63840359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定