Showing posts with label toString. Show all posts
Showing posts with label toString. Show all posts

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;
    }
 
}
 
 

Wednesday, September 23, 2009

Java - cast int to string and string to int


public class Main {

    public static void main(String[] args) {
        String s = "23";
        int i = 22;

        // int to string
        String iString = Integer.toString(i);
        System.out.println(iString);
        // output:
        //  22

        // string to int
        int sInt = Integer.parseInt(s);
        System.out.println(sInt);
        // output:
        //  23
    }
}