Prev: Your Helping Heart
Next: create folder
From: Simone on 19 Jul 2010 06:56 Hello, maybe my question has already been asked here but I couldn't find a precise answer for now... Is it possible to customize the way the implicit "toString" method, invoked e.g. by System.out.print(...), will produce the output string? If I write the following code: int[] vector = new int[] {1,2,3,4}; System.out.println( vector ); the output is: v = [I(a)1d9f953d corresponding to an internal java information about the object (the hash code, I believe). With a "classic" object of a class, I can rewrite the toString() method and output the string that I want, simply writing e.g. System.out.println( objectName ). Is there a way to rewrite the "implicit" toString() method for arrays, so that I can write System.out.println( "v =" + vector ) and obtain the output: v = (1,2,3,4) ? Many thanks in advance. Simone.
From: Tom Anderson on 19 Jul 2010 07:53 On Mon, 19 Jul 2010, Simone wrote: > Is there a way to rewrite the "implicit" toString() method for arrays, > so that I can write System.out.println( "v =" + vector ) and obtain the > output: v = (1,2,3,4) ? Sadly not. The best sticking-plaster is usually to wrap the array in a list: System.out.println(Arrays.asList(vector)); That method returns a lightweight wrapper around the array that, amongst other things, implements a more sensible toString. tom -- Better to die on your feet than live on your knees. -- Emiliano Zapata
From: Simone on 22 Jul 2010 17:49 First, thank you Stefan and Tom for your answers. I apologize for answering so late. I also noticed that the solution with Arrays.asList(vector) works only if vector is an array of a wrapper class and not if it's an array of a primitive type, in which case the output is, again, the hash code of the array. I'm using the solution Arrays.asList(vector) for now, maybe I'll write a specific method... Or I'll write a wrapper class for my array with my specific toString... unfortunately I'll lose the chance to use the brackets for referring to elements. Simone. On 19 Lug, 13:53, Tom Anderson <t...(a)urchin.earth.li> wrote: > On Mon, 19 Jul 2010, Simone wrote: > > Is there a way to rewrite the "implicit" toString() method for arrays, > > so that I can write System.out.println( "v =" + vector ) and obtain the > > output: v = (1,2,3,4) ? > > Sadly not. > > The best sticking-plaster is usually to wrap the array in a list: > > System.out.println(Arrays.asList(vector)); > > That method returns a lightweight wrapper around the array that, amongst > other things, implements a more sensible toString. > > tom > > -- > Better to die on your feet than live on your knees. -- Emiliano Zapata
From: Alan Gutierrez on 22 Jul 2010 18:12 Simone wrote: > First, thank you Stefan and Tom for your answers. I apologize for > answering so late. > > I also noticed that the solution with Arrays.asList(vector) works only > if vector is an array of a wrapper class and not if it's an array of a > primitive type, in which case the output is, again, the hash code of > the array. > > I'm using the solution Arrays.asList(vector) for now, maybe I'll write > a specific method... Or I'll write a wrapper class for my array with > my specific toString... unfortunately I'll lose the chance to use the > brackets for referring to elements. Don't top post. Not sure what you're on about: import java.util.Arrays; public class AsList { public static void main(String[] args) { System.out.println(Arrays.asList(1, 2, 3)); } } [alan(a)postojna ~]$ javac AsList.java [alan(a)postojna ~]$ java AsList [1, 2, 3] [alan(a)postojna ~]$ -- Alan Gutierrez - alan(a)blogometer.com - http://twitter.com/bigeasy
From: John B. Matthews on 22 Jul 2010 18:14
In article <09545a75-b1e3-4465-ad92-d1c9ea77c261(a)5g2000yqz.googlegroups.com>, Simone <simbo1980(a)gmail.com> wrote: > I also noticed that the solution with Arrays.asList(vector) works only > if vector is an array of a wrapper class and not if it's an array of a > primitive type, in which case the output is, again, the hash code of > the array. Would this be an usable alternative? int[] intArray = new int[]{ 1, 2, 3, 4 }; System.out.println(Arrays.toString(intArray)); For better or worse, the square brackets are included: [1, 2, 3, 4] -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews> |