创建一个具有 10 个输入的 String 类型数组

问题描述 投票:0回答:3

我想练习数组,但我不明白为什么它不打印以下内容: 问题是终端输出:null 这是为什么?我做错了什么?

package Practice;

public class CreatingArrays {
    public static void main(String[] args) {
        String[] women = { "persian", "palestinian", "german", "russian", "spanish", "italian", "greek", "hungarian",
                "brazilian", "turkish" };
        women = new String[10];

        System.out.println(women[8]);

    }
}
java arrays string new-operator
3个回答
1
投票

在打印到控制台之前,您正在将 Women 重新分配为空字符串 [10]。 删除

women = new String[10];
,它应该可以正常工作。


0
投票
String[] women = { "persian", "palestinian", "german", "russian",
"spanish", "italian", "greek", "hungarian","brazilian", "turkish" };

System.out.println(women[8]);

将为您工作。


0
投票

公共类创建数组{ 公共静态无效主(字符串[] args){

    String[] women = { "persian", "palestinian", "german", "russian", "spanish", "italian", "greek", "hungarian",
            "brazilian", "turkish" };
            
    System.out.println(women[8]);
}

}

© www.soinside.com 2019 - 2024. All rights reserved.