How to quickly print arrays?

C

Charles Fox

Is there some little utility function that will let me do
System.out.println(magicfunction(a))

where a is an array of doubles, ints, or bytes,
and it prints a nice pretty representation of the contents of a:

[ 1 , 2 , 3 , 4]

I've written a few of these myself but I dont want to reinvent the wheel here!
 
M

Murray

Charles Fox said:
Is there some little utility function that will let me do
System.out.println(magicfunction(a))

where a is an array of doubles, ints, or bytes,
and it prints a nice pretty representation of the contents of a:

[ 1 , 2 , 3 , 4]

I've written a few of these myself but I dont want to reinvent the wheel
here!

I don't believe there's anything in the JDK, but you could try the Jakarta
Commons Lang package. It has a toString() method in ArrayUtils that handles
primitive and Object arrays (even multidimensional). The output looks like
{1,2,3,4} but if you need something more specific you can use
ToStringBuilder which is in the same package.

http://jakarta.apache.org/commons/lang/
 
C

Chris Smith

Charles said:
Is there some little utility function that will let me do
System.out.println(magicfunction(a))

where a is an array of doubles, ints, or bytes,
and it prints a nice pretty representation of the contents of a:

[ 1 , 2 , 3 , 4]

I've written a few of these myself but I dont want to reinvent the wheel here!

I'm not aware of such a thing offered as a third-party library. It's so
trivial that the challenge of deploying the library would dwarf writing
it yourself, so we're pretty much doomed to have people privately invent
their own copies until Sun sees fit to include this in the core API.
(Feel free to submit an RFE for this, by all means.)

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Joona I Palaste

Charles Fox said:
Is there some little utility function that will let me do
System.out.println(magicfunction(a))
where a is an array of doubles, ints, or bytes,
and it prints a nice pretty representation of the contents of a:
[ 1 , 2 , 3 , 4]
I've written a few of these myself but I dont want to reinvent the wheel here!

What's wrong with System.out.println(Arrays.asList(a))?
 
R

Real Gagnon

Chris Smith said:
privately invent their own copies until Sun sees fit to include this
in the core API. (Feel free to submit an RFE for this, by all means.)

Actually, nice array printing is in 1.5 :

public class Test {
public static void main(String args[]) {
String s[] = {"a", "b", "c", "d"};
double[][] d = {
{0.50, 0.70, 0.40, 0.60},
{0.50, 1.10, 0.50, 0.80}
};
System.out.println(java.util.Arrays.toString(s));
System.out.println(java.util.Arrays.deepToString(d));
// output
// [a, b, c, d]
// [[0.5, 0.7, 0.4, 0.6], [0.5, 1.1, 0.5, 0.8]]
}
}

Bye.
 
N

Neal Gafter

Charles said:
Is there some little utility function that will let me do
System.out.println(magicfunction(a))

where a is an array of doubles, ints, or bytes,
and it prints a nice pretty representation of the contents of a:

[ 1 , 2 , 3 , 4]

I've written a few of these myself but I dont want to reinvent the wheel here!

in JDK1.5, Arrays.toString(a)
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top