Thursday, September 24, 2009

Java - create and use a String or int array

 
public class Main {
    public static void main(String[] args) {
        // standard, fast, old school array 
        // string array 
        String[] stringArray = new String[10];
        stringArray[0] = "first item in array";
        System.out.println("array length: " + stringArray.length);
        // indicates:  10 
        // even if you've only populated 
        // one of the you allocated room for 10 
 
        // you can also for loop through and populate 
        String tempString = "";
        for (int i=0; i<stringArray.length; i++){
            tempString = Integer.toString(i+1);
            stringArray[i] = "arrayitem " + tempString;
        }
 
        // display contents of array 
        //  with a for each 
        for (String s:stringArray){
            System.out.println(s);
        }
 
 
        // same methodology for an int array 
        int[] intArray = new int[10];
        intArray[0] = 213;
    }
 
}
 
 

No comments:

Post a Comment