How do you define a string array in Java?

In Java, you can define a string array as follows:

String[] strArray = new String[3]; // 创建一个长度为3的字符串数组

strArray[0] = "Hello";
strArray[1] = "World";
strArray[2] = "Java";

// 或者在定义数组的同时初始化
String[] strArray2 = {"Hello", "World", "Java"};

The code above initializes an array with three string elements and assigns them corresponding values. It is important to note that in Java, array indexes start from 0.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds