What's a quick way to output the values in an array?
Author: Deron Eriksson
Description: This Java tutorial describes the use of ArrayUtils to output the values in an array.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page:    1 2 >

The ArrayUtils class in the Jakarta Commons LangS library has a variety of useful methods for working with arrays. The Jakarta CommonsSW Lang library can be downloaded from http://commons.apache.org/downloads/download_lang.cgi. This tutorial will utilize the ArrayUtils and ToStringBuilder classes in Commons Lang.

I added the commons-lang-2.3.jar file to my project's lib directory.

'testing' project

Next I added the commons-lang-2.3.jar file to my project's build path.

commons-lang-2.3.jar added to build path

For simplicity, the ArrayTest class contains an inner class called TestClass. I'll get back to TestClass soon. In ArrayTest's main method, we first declare and initialize an int array. We display intArray's default toString value, and we then call ArrayUtils.toString(intArray), which displays the int values within intArray.

Following this, we perform similar tasks with a String array. The results are basically identical.

The next task is more interesting. We create two TestClass objects and add these to a TestClass array. We output the testClassArray's default toString value, and then we output ArrayUtils.toString(testClassArray). If we didn't write a toString() method for TestClass, the ArrayUtils.toString(testClassArray) call would result in a display of the default toString values of each TestClass object within the array. This would not let us see the field values within the TestClass objects.

However, notice that our TestClass inner class has had its toString() method overridden, and it contains a call to ToStringBuilder.reflectionToString(this). This call returns the names and values of the fields within the TestClass object. As a result of this, the call to ArrayUtils.toString(testClassArray) results in a call to the toString method of each TestClass object, so we are able to view all of the field values of all of the objects in the array.

ArrayTest.java

package test;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

public class ArrayTest {

	private class TestClass {
		private String val1;
		private String val2;

		public TestClass(String v1, String v2) {
			val1 = v1;
			val2 = v2;
		}

		public String getVal1() {
			return val1;
		}

		public String getVal2() {
			return val2;
		}

		public String toString() {
			return "\n" + ToStringBuilder.reflectionToString(this);
		}
	}

	public static void main(String[] args) {
		int[] intArray = { 1, 2, 3, 4, 3, 2, 1 };
		System.out.println("intArray: " + intArray);
		System.out.println("intArray values: " + ArrayUtils.toString(intArray));

		String[] stringArray = { "Hello", "Goodbye", "Later" };
		System.out.println("\nstringArray: " + stringArray);
		System.out.println("stringArray values: " + ArrayUtils.toString(stringArray));

		ArrayTest at = new ArrayTest();
		TestClass tc1 = at.new TestClass("my value 1", "my value 2");
		TestClass tc2 = at.new TestClass("another value 1", "another value 2");
		TestClass[] testClassArray = { tc1, tc2 };
		System.out.println("\ntestClassArray: " + testClassArray);
		System.out.println("testClassArray values: " + ArrayUtils.toString(testClassArray));
	}
}

(Continued on page 2)

Page:    1 2 >