Think in java 答案

    技术2022-05-11  109

    阅前声明: 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.


    最新回复(0)