user7146946 user7146946 - 1 year ago 60
Java Question

How to remove the last "->" in a word ladder

I am printing out elements from an array list and I want to have "->" in between each word except the last word. My current code is as showed below:

for(int m = result.size()-1; m > -1; m--)
System.out.print(result.get(m) + " -> ");

Actual output from my code:

Path of word ladder: abase -> abash ->

Expected output:

Path of wordladder: abase -> abash

Answer Source

For any Java versions:

 System.out.print(result.get(m) + (m==0?"":" -> "));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download