How to define a string array and assign values in Java.

In Java, you can define a string array and assign values to it, as shown below:

// 定义一个字符串数组
String[] fruits = new String[3];

// 为字符串数组赋值
fruits[0] = "apple";
fruits[1] = "banana";
fruits[2] = "orange";

// 打印数组中的值
for (int i = 0; i < fruits.length; i++) {
    System.out.println(fruits[i]);
}

In the example above, we initially defined a string array named ‘fruits’ with a length of 3. We then assigned different strings to each element of the array. Finally, we used a for loop to print the value of each element in the array.

bannerAds