gudii9
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
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
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));
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public String toString() {
return "\n" + ToStringBuilder.reflectionToString(t his);
}
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(testCl assArray)) ;
After initializing and setting inner class why the output is coming as
ArrayTest$TestClass@4318f3
ArrayTest$TestClass@36867e
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.