如何给Java String数组赋值

时间:2024-11-12 15:10:08
//一维数组

String[] str = new String[5]; //创建一个长度为5的String(字符串)型的一维数组

str[0] = "a"; //赋值

str[1] = "b";

str[2] = "c";

str[3] = "d";

str[4] = "e";

//也可以创建并赋值

String[] str = {"a","b","c","d","e"};



//二维数组

String[][] str = new String[2][2]; //创建一个2行2列的二维数组

str[0][0] = "a0"; //赋值

str[0][1] = "a1";

str[1][0] = "b0";

str[1][1] = "b1";

//也可以创建并赋值

String[][] str = {{"a0","a1"},{"b0","b1"}};



总结:

类型名[]/[][] 变量名 = new 类型名[]/[][];