Solved

array example

Posted on 2014-01-31
5
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

627 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