import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListDemo
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
//declare ArrayList
ArrayList<String> inputWords=new
ArrayList<String>();
System.out.println("Enter line: ");
String line=input.nextLine();
//split the line
String[] words=line.split(" ");
for(String word:words)
inputWords.add(word);
//sort the words
Collections.sort(inputWords);
//print the sorted words
for(String w:inputWords)
System.out.print(w+" ");
}
}
Output
Get Answers For Free
Most questions answered within 1 hours.