gpt4 book ai didi

java - 这只是一个错字吗?

转载 作者:行者123 更新时间:2023-12-01 11:09:03 26 4
gpt4 key购买 nike

我最近一直在尝试学习 Java,虽然进展缓慢,但我已经成功了。不管怎样,我一直在读这本书:“Sams 在 24 小时内自学 Java”,并且很早就指出:

Java is flexible about where the square brackets are placed when an array is being created. You can put them after the variable name instead of the variable type, as in the following: String niceChild[];

To make arrays easier for humans to spot in your programs, you should stick to one style rather than switching back and forth. Programs that use arrays in this book always place the brackets after the variable or object type.

我完全理解所有这些,但后来在其中一个练习中,它似乎与刚才所说的相悖。正如您在下面看到的那样,它有: Stringphrase[] = { 但我不禁认为它应该是 String[]phrase = { ?我知道该程序可以很好地运行。我认为这只是一个错字,但我想从更了解它的人那里了解情况。

        package com.java24hours;

class Wheel {
public static void main(String[] arguments) {
String phrase[] = {
"A STITCH IN TIME SAVES NINE",
"DON'T EAT YELLOW SNOW",
"JUST DO IT",
"EVERY GOOD BOY DOES FINE",
"I WANT MY MTV",
"I LIKE IKE",
"PLAY IT AGAIN, SAM",
"FROSTY THE SNOWMAN",
"ONE MORE FOR THE ROAD",
"HOME FIELD ADVANTAGE",
"VALENTINE'S DAY MASSACRE",
"GROVER CLEVELAND OHIO",
"SPAGHETTI WESTERN",
"AQUA TEEN HUNGER FORCE",
"IT'S A WONDERFUL LIFE"
};
int[] letterCount = new int[26];
for (int count = 0; count < phrase.length; count++) {
String current = phrase[count];
char[] letters = current.toCharArray();
for (int count2 = 0; count2 < letters.length; count2++) {
char lett = letters[count2];
if ( (lett >= 'A') & (lett <= 'Z') ) {
letterCount[lett - 'A']++;
}
}
}
for (char count = 'A'; count <= 'Z'; count++) {
System.out.print(count + ": " +
letterCount[count - 'A'] +
" ");
if (count == 'M') {
System.out.println();
}
}
System.out.println();
}
}

最佳答案

你实际上可以这样做:

String[] names = { "James", "Bond"};//推荐这样做

String names[] = { "James", "Bond"};//这是合法的,但可读性较差。

关于java - 这只是一个错字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32586181/

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