您的位置:首页 > 编程语言 > Java开发

Think in java 答案_Chapter 4_Exercise 5

2007-01-22 14:10 399 查看
阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx

/****************** Exercise 5 ******************
* Create an array of String objects and assign a
* string to each element. Print the array using
* a for loop.
***********************************************/
public class E05_StringArray {
public static void main(String args[]) {
// Doing it the hard way:
String sa1[] = new String[4];
sa1[0] = "These";
sa1[1] = "are";
sa1[2] = "some";
sa1[3] = "strings";
for(int i = 0; i < sa1.length; i++)
System.out.println(sa1[i]);
// Using aggregate initialization to
// make it easier:
String sa2[] = {
"These", "are", "some", "strings"
};
for(int i = 0; i < sa2.length; i++)
System.out.println(sa2[i]);
}
}

//+M java E05_StringArray

**The above solution shows both ways you can do it: explicitly creating the array object and assigning a string into each slot by hand, or using aggregate initialization, which creates the array object and does the initialization for you.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: