Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

array example

Posted on 2014-01-31
5
Medium Priority
?
323 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 400 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 600 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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

715 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