Solved

array example

Posted on 2014-01-31
5
316 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 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

16 Experts available now in Live!

Get 1:1 Help Now