public class StringArray {

    public static void main (String [] args) {
	StringBuffer [] sb = new StringBuffer [3];
	
	// print out the "elements":
	for (int i = 0; i < sb.length; ++i) {
	    System.out.println("sb[" + i + "] = " + sb[i]);
	    // trying to do smth with the element (causes run-time error):
	    // System.out.println("sb[" + i + "] = " + sb[i].reverse());
	}

	// create the strings themselves:
	for (int i = 0; i < sb.length; ++i) {
	    sb[i] = new StringBuffer(" little monkeys");
	    sb[i].insert(0,i);
	}

	// after the string buffers are created:
	System.out.println("StringBuffers have been created:");
	for (int i = 0; i < sb.length; ++i) {
	    System.out.println("sb[" + i + "] = " + sb[i]);
	}
	
	// reverse the string buffers:
	for (int i = 0; i < sb.length; ++i) {
	    sb[i].reverse();
	}

	// after the string buffers are reversed:
	System.out.println("StringBuffers have been reversed:");
	for (int i = 0; i < sb.length; ++i) {
	    System.out.println("sb[" + i + "] = " + sb[i]);
	}
    }

}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.