在Java中,有以下幾種方式來定義字符串數組:
String[] strArray = {"Hello", "World"};
String[] strArray = new String[3];
strArray[0] = "Hello";
strArray[1] = "World";
strArray[2] = "!";
String[] strArray = (String[]) Array.newInstance(String.class, 3);
strArray[0] = "Hello";
strArray[1] = "World";
strArray[2] = "!";
需要注意的是,以上方式都是定義一個一維字符串數組。如果需要定義多維字符串數組,可以通過嵌套以上方式來實現。例如:
String[][] strArray = {{"Hello", "World"}, {"Java", "Programming"}};