我一直在做一些基本的教程。其中一个要求我设置一个数组来保存以下字符串值:
Beyonce (f)
David Bowie (m)
Elvis Costello (m)
Madonna (f)
Elton John (m)
Charles Aznavour (m)
写一个程序循环,计数有多少男性歌手和多少是女性,并在控制台中显示我的答案。
我设法完成它,但我设置我的数组的方式不同于提供的答案。
矿山如下:
String names[] = {"Beyonce (f)", "David Bowie (m)", "Elvis Costello (m)", "Madonna (f)", "Elton John (m)", "Charles Aznavour (m)"};
提供的答案是:
String[] singers = new String[6];
singers[0] = "Beyonce (f)";
singers[1] = "David Bowie (m)";
singers[2] = "Elvis Costello (m)";
singers[3] = "Madonna (f)";
singers[4] = "Elton John (m)";
singers[5] = "Charles Aznavour (m)";
我应该创建一个“新”数组吗?如果是,为什么?两者之间有什么区别?