Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

array example

Hi,

i was trying below example
http://www.avajava.com/tutorials/lessons/whats-a-quick-way-to-output-the-values-in-an-array.html?page=1
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));
	}
}

Open in new window


i have not understod what author is trying to mention in this example.

what is happening in below 3 lines

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

please advise
Any links resources ideas highly appreciated. Thanks in advance
SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krakatoa
The example seems to just boil down to an illustration of the reflective use of

ToStringBuilder.reflectionToString(this);

Open in new window


which is essentially a way for the interpreter to get hold of the values (fields) in an object at runtime, so instead of iterating through the fields explicitly, this function does that bit for you.

The three lines you mention instantiate an inner class, and set its fields.
Avatar of gudii9

ASKER

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

what is happening in above lines.

Also
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));

After initializing and setting inner class why the output is coming as
ArrayTest$TestClass@4318f375[val1=my value 1,val2=my value 2],
ArrayTest$TestClass@36867e89[val1=another value 1,val2=another value 2]}

also

     TestClass[] testClassArray = { tc1, tc2 };
what is happening in above line. Sending both inner classes into an array?
How is TestClass[](array) different from TestClass(.java class). Where we defined TestClass[] array.
Please advise
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial