/home/wpollock1/public_html/AJava/Destutter.java
import java.util.*;
/** Destuddering a list means to remove adjacent duplicates,
* for example [1, 4, 2, 2, 3, 3, 5] ==> [1, 4, 2, 3, 5]
* (For example, removing extra spaces between words in a document,
* removing duplicates from a sorted list (producing a "set"), etc.)
* In this case, the list is the command-line arguments (Strings).
*
* @author Wayne Pollock, Tampa Florida USA
*/
public class Destutter {
public static void main ( String [] args ) {
String previous = null;
List<String> result = new ArrayList<>();
for ( String arg : args ) {
if ( ! arg.equals( previous ) ) {
result.add( arg );
previous = arg;
}
}
// Display resulting list with adjacent duplicates removed:
// (The argument to result.toArray just tells the JVM the type of the
// resulting array; see the Java docs for more info on that.)
System.out.println( Arrays.toString( result.toArray(new String[0]) ) );
}
}