user7146946 user7146946 - 21 days ago 9
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

For any Java versions:

 System.out.print(result.get(m) + (m==0?"":" -> "));
Comments