gpt4 book ai didi

Java 找不到任何数组的符号?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:56:57 24 4
gpt4 key购买 nike

所以,这很烦人。

我制作了 2 个程序。

#1:

class Arrays2 {
public static void main(String[] args){

String sentenceBest[] = {"This is the first sentence!"};

char chR[] = sentenceBest.toCharArray();

for (int counter = 0; counter < chR.length; counter++){
char now = chR[counter];
if (now != ' ') {
System.out.println(now);
}else {
System.out.println('.');
}
}



}
}

对于这个程序,它说:

Arrays_ToCjarArray(not working).java:6: cannot find symbol
symbol : method toCharArray()
location: class java.lang.String[]
char chR[] = sentenceBest.toCharArray();
^
1 error

#2 计划:

class Arrays_3 {
public static void main(String[] args){

boolean numbers[] [] = new boolean[10] [10];

numbers[9] [8] = true;

System.out.println(numbers[9][8] + "!!!");


String names[] = {"Marton", "Balint", "Thomas", "David", "John", "Peter", "Andy", "Daniel", "Josh", "James", "Erling", "Romeo", "Vincent", "Fabian"};

System.out.println("The origional order: ");
for (int counter = 0; counter < names.length; counter++){
String newName = names[counter];
System.out.println(counter + ": " + newName);
}


System.out.println("The Alphabetical order: ");

Arrays

for (int counter2 = 0; counter2 < names.length; counter2++) {
System.out.println(counter2 + ": " + names);
}


}
}

同样的事情。找不到标志。太烦人了。

Arrays_3.java:21: cannot find symbol
symbol : variable Arrays
location: class Arrays_3
Arrays.sort(names);
^
1 error

我真的不明白这一点,因为这个源代码来自一本很棒的书(到目前为止),书名是 Sams 在 24 小时内教你 Java。所以我真的不明白这一点。我们将不胜感激任何帮助。

最佳答案

1. toCharArray()适用于 String 而不是 String[]

2.你需要导入Arrays通过添加

import java.util.Arrays;

关于Java 找不到任何数组的符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17532910/

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