Solved

array example

Posted on 2014-01-31
5
317 Views
Last Modified: 2014-02-03
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
0
Comment
Question by:gudii9
5 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 100 total points
ID: 39825421
Can you be more specific about what part you didn't understand? It just looks to me like two instances of the TestClass class are created, then a two-element array containing those instances is created.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39825453
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39825503
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
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 150 total points
ID: 39825567
After initializing and setting inner class why the output is coming as

because that is the result of the reflective method's call. It grabs the references, and formats them in that output.
0
 
LVL 6

Accepted Solution

by:
Mahesh Bhutkar earned 250 total points
ID: 39829439

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

Because System.out.println(obj); method internally invokes toString() method of Object class. For more information you may refer Object.java source code.

TestClass[] testClassArray = { tc1, tc2 };
tc1 & tc2 are instance variables of TestClass which is inner class.  whereas this statement creates array of TestClass reference by testClassArray variable.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now