Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic