toString equivalent for array

P

Ph. Barthelemy

Hi,

Sorry for asking a lame question, but I googled to find the answer and
did not find any.

is there a nice toString method for array ?
something that will concatenate the toString output of each element of
the array ?

calling the toString on a array just output gibberish ( an pointer
maybe ), but does not have the result explained above.

My problem is that I upcast objects of several Classes to Object ( the
java root class ). then I call toString on the Objects. the result is
not nice for arrays...
can I do something generic that will handle array as well as Classes ?

How can I does it ?

TIA,
--Philippe
 
P

Paul Lutus

Ph. Barthelemy said:
Hi,

Sorry for asking a lame question, but I googled to find the answer and
did not find any.

is there a nice toString method for array ?
something that will concatenate the toString output of each element of
the array ?

Why not write one? This is why computer languages allow programmers to
change what they do.
calling the toString on a array just output gibberish

It's not gibberish.
( an pointer
maybe ), but does not have the result explained above.

My problem is that I upcast objects of several Classes to Object ( the
java root class ). then I call toString on the Objects. the result is
not nice for arrays...

Define "not nice". What do you want? What do you get? How do they differ?
 
T

Thomas Fritsch

Ph. Barthelemy said:
Hi,

Sorry for asking a lame question, but I googled to find the answer and
did not find any.

is there a nice toString method for array ?
something that will concatenate the toString output of each element of
the array ?

calling the toString on a array just output gibberish ( an pointer
maybe ), but does not have the result explained above.

My problem is that I upcast objects of several Classes to Object ( the
java root class ). then I call toString on the Objects. the result is
not nice for arrays...
can I do something generic that will handle array as well as Classes ?

How can I does it ?

TIA,
--Philippe

May be, instead of a plain array you want to use an ArrayList, which has
this kind of a "nice" toString method.
See
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/AbstractCollection.html#toString()>
and <http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html>
 
P

Ph. Barthelemy

Paul said:
Why not write one? This is why computer languages allow programmers to
change what they do.
I do not want to reinvent the wheel over again. I assumed what I want to
do must have been done somewhere.
It's not gibberish. okay, agreed.

Define "not nice". What do you want? What do you get? How do they differ?
As I said, nice to me would be :
thanks,
--Philippe
 
E

Elliott Back

Ph. Barthelemy said:
Hi,

Sorry for asking a lame question, but I googled to find the answer and
did not find any.

is there a nice toString method for array ?
something that will concatenate the toString output of each element of
the array ?

calling the toString on a array just output gibberish ( an pointer
maybe ), but does not have the result explained above.

My problem is that I upcast objects of several Classes to Object ( the
java root class ). then I call toString on the Objects. the result is
not nice for arrays...
can I do something generic that will handle array as well as Classes ?

How can I does it ?

TIA,
--Philippe

Just add a method in a wrapper class like:

public String toString(){
String temp = "";

for(int i = 0; i < this.object.length - 1; i++){
temp += i + ", ";
}

temp += this.object[this.object.length - 1];
return temp;
}
 
M

Mike Schilling

Ph. Barthelemy said:
Hi,

Sorry for asking a lame question, but I googled to find the answer and
did not find any.

is there a nice toString method for array ?
something that will concatenate the toString output of each element of
the array ?

calling the toString on a array just output gibberish ( an pointer
maybe ), but does not have the result explained above.

My problem is that I upcast objects of several Classes to Object ( the
java root class ). then I call toString on the Objects. the result is
not nice for arrays...
can I do something generic that will handle array as well as Classes ?

Generic Collections have a "nice" toString(0 method that concatentates the
string values of the collection's members. The simplest way to get thei
behavior for arrays is

import java.util.Arrays;

Object[] arr;
Arrays.asList(arr).toString();

Note: this works for arrays of Object (or a subclass thereof). I don't know
of anything equivalent for arrays of scalar types.
 
G

Guest

Ph. Barthelemy said:
Hi,

Sorry for asking a lame question, but I googled to find the answer and
did not find any.

is there a nice toString method for array ? something that will
concatenate the toString output of each element of the array ?

calling the toString on a array just output gibberish ( an pointer maybe
), but does not have the result explained above.

My problem is that I upcast objects of several Classes to Object ( the
java root class ). then I call toString on the Objects. the result is
not nice for arrays...
can I do something generic that will handle array as well as Classes ?

How can I does it ?

TIA,
--Philippe

Just add a method in a wrapper class like:

public String toString(){
String temp = "";

for(int i = 0; i < this.object.length - 1; i++){
temp += i + ", ";
}
}
temp += this.object[this.object.length - 1]; return temp;
}

Use a StringBuffer or StringBuilder while constructing the result.

La'ie Techie
 
T

Tony Morris

Arrays.toString(...)

Ignore suggestions to write it yourself - duplicating core API functionality
is almost always a bad thing, and certainly in this case.
 
W

Will Hartung

Tony Morris said:
Arrays.toString(...)

Ignore suggestions to write it yourself - duplicating core API functionality
is almost always a bad thing, and certainly in this case.

Is that a JDK 5 thing? I don't see it in 1.4.

Regards,

Will Hartung
([email protected])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top