permaculture playing cards*
The moose likes Java in General and the fly likes java.lang.reflect.Array.getLength(aA) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.reflect.Array.getLength(aA)" Watch "java.lang.reflect.Array.getLength(aA)" New topic
Author

java.lang.reflect.Array.getLength(aA)

Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147

I have the code, please see below and I need to convert the aA to be a string. Please help.

int num = java.lang.reflect.Array.getLength(aA);
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19062
    
  40

Kee Kee moon wrote:
I have the code, please see below and I need to convert the aA to be a string. Please help.

int num = java.lang.reflect.Array.getLength(aA);


Well, in this case, "aA" is an object array -- and you need to define how to convert that to a string first. The toString() method of an array just reports the type of the array and the indentity hash, which I am assuming is not what you want.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
j
System.out.println(aA.toString()) and I got the following print out
ava.lang.String;@165f738

No, that is not what I want. Do you know anyway I can print out a readable string, such as "Mar022012_data_string" which is readable and meaningful.

Thanks


Henry Wong wrote:
Kee Kee moon wrote:
I have the code, please see below and I need to convert the aA to be a string. Please help.

int num = java.lang.reflect.Array.getLength(aA);


Well, in this case, "aA" is an object array -- and you need to define how to convert that to a string first. The toString() method of an array just reports the type of the array and the indentity hash, which I am assuming is not what you want.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19062
    
  40

Kee Kee moon wrote:j
System.out.println(aA.toString()) and I got the following print out
ava.lang.String;@165f738

No, that is not what I want. Do you know anyway I can print out a readable string, such as "Mar022012_data_string" which is readable and meaningful.

Thanks


As mentioned, you need to make that definition -- and then implement it. How can the program know what is "meaningful"? That is a higher level specific term.

For example, if it is an array of strings, and the individuals strings are meaningful, one option is to cast it to an object array, and then iterate through the elements printing out each element. You need to define what is "meaningful", and then you need to implement it.

Henry
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
Meaningful means readable like "Mar022012_data_string"

Henry Wong wrote:
Kee Kee moon wrote:j
System.out.println(aA.toString()) and I got the following print out
ava.lang.String;@165f738

No, that is not what I want. Do you know anyway I can print out a readable string, such as "Mar022012_data_string" which is readable and meaningful.

Thanks


As mentioned, you need to make that definition -- and then implement it. How can the program know what is "meaningful"? That is a higher level specific term.

For example, if it is an array of strings, and the individuals strings are meaningful, one option is to cast it to an object array, and then iterate through the elements printing out each element. You need to define what is "meaningful", and then you need to implement it.

Henry
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Okay. Then here's your code:


That was what you wanted, right? You can't override the toString() method of an array so you have to write your own method which makes the array readable.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kee Kee moon wrote:Meaningful means readable like "Mar022012_data_string"


So, what's stopping your from writing the code to take an array and generate a "meaningful" String from it?

I think the answer to that question is that you have not yet taken the time to think about a precise definition of what a "meaningful" String would look like for any possible array in your domain. Once you do that, if you've used Java arrays at all, I think you'll find that translating that description to Java is quite easy, and takes only a few lines of code.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8419
    
  23

Kee Kee moon wrote:
System.out.println(aA.toString()) and I got the following print out
ava.lang.String;@165f738

No, that is not what I want. Do you know anyway I can print out a readable string, such as "Mar022012_data_string" which is readable and meaningful.

So, what do you want? You have to be explicit.

If you want a string of the contents of your array try
Arrays.toString(a);
otherwise, if you want some idea of what you named it in your program, I'm not sure there is a solution (although there might be with reflection).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.reflect.Array.getLength(aA)