views:

1771

answers:

7
+5  Q: 

Java: print_r?

Hi guys,

last time I asked how to populate a data structure here. Now I would like to know if there's something in Java, like the print_r I use in PHP, to represent what I have populated in the Maps and lists without having to do my own algorithm.

Any ideas?

+5  A: 

Calling toString on the collection should return a string containing all the elements string representations.

This won't work with built-in arrays though, as they don't have a toString override and will just give you a memory address.

workmad3
A: 

I don't know of an equivalent of print_r in java.

But...

Every object has a default implementation of the toString() method, the list and map implementations print their contents if you call toString() for them.

If you need any debug information printed out, toString() may be the place you are looking for.

dhiller
+5  A: 

You might try the good old Apache Commons Lang's ToStringBuilder.

Tim Mooney
A: 

There is no equivalent for print_r in Java.

But for maps or lists you can use the foreach-loop like this:

List <String> list = …; // fills your list 

// print each list element 

for (String s : list) {

   System.out.println(s);

 }
DeeCee
you could also use an Iterator for this..List <String> list = ...;Iterator iterator;iterator = list.iterator();while (iterator.hasNext()) { System.out.println(iterator.next() + ", ");}
ninuhadida
the foreach loop in java is (IMO anyway) a much cleaner syntax than iterators, and I believe it uses iterators under the surface anyway.
workmad3
A: 

For arrays the easiest way is Arrays.asList(array).toString() . I generally implement toString() on objects I like to habe in debug outputs. For generated objects (JAXB etc.) though might need to make an utility class to print it.

hstoerr
As of Java 1.5, you can use Arrays.toString(array) instead.
Michael Myers
+2  A: 

Others have mentioned the toString() method. This is really only useful when the object implements toString(). If it has not been implemented properly you will end up with something like this: java.lang.Object@1b9240e. Even if it is a little tedious it is still easy to implement toString() in your own classes, but third party classes will not always implement it. You are much better off using a debugger. The only reason things like print_r even exist in PHP is because of the lack of a real debugger. I think you will find that being able to set breakpoints instead of using a bunch of diagnostic print statements will result in a much faster workflow and cleaner code.

jm4
hi mate :) Yep, I wanted something like print_r because I hadn't thought about using a debugger since as you said, I've never used one with PHP. I'll see how I can use a debugger with with Java! :)
ninuhadida
A decent set of logging is invaluable when used with a debugger as it allows you to trace down to a certain function a lot quicker than stepping through with a debugger. Logging is also useful in production mode code as it is frequently then the only record of an issue.
workmad3
+2  A: 

Depending on exactly what you want to do, the solution could be fairly simple. The following won't produce the formatted output that print_r provides, but it will allow you to output the structure of lists, arrays, and maps:

    // Output a list
    final List<String> list = new ArrayList<String>();
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");
    System.out.println(list);

    // Output an array
    final String[] array = {"four", "three", "two", "one"};
    System.out.println(Arrays.asList(array));

    // Output a map
    final Map<String, String> map = new HashMap<String, String>();
    map.put("one", "value");
    map.put("two", "value");
    map.put("three", "value");
    System.out.println(map.entrySet());

For other types of objects you could use reflection to create a utility for this purpose.

laz
cool thanks for the tip dude :)
ninuhadida