0% found this document useful (0 votes)
25 views

Printlargest (Arrays - Aslist (Arr) ) ) Static Void

This Java program accepts integer inputs from the user, stores them in a string array, sorts the array using a custom comparator that concatenates two strings and compares them, and prints out the largest number formed by concatenating the sorted string elements. It defines a main method that gets inputs, a printLargest method that sorts and prints the array, and an anonymous inner class that implements the comparator used for sorting.

Uploaded by

Swati Kar
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Printlargest (Arrays - Aslist (Arr) ) ) Static Void

This Java program accepts integer inputs from the user, stores them in a string array, sorts the array using a custom comparator that concatenates two strings and compares them, and prints out the largest number formed by concatenating the sorted string elements. It defines a main method that gets inputs, a printLargest method that sorts and prints the array, and an anonymous inner class that implements the comparator used for sorting.

Uploaded by

Swati Kar
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 1

//Maximum Number

public class Prog3 {
 public static void main (String[] args){



Scanner sc=new Scanner(System.in);
 System.out.println("Enter
input: ");
 int n=sc.nextInt();
 String[] arr = new String[n];

for (int i=0;
 i<n;
 i++){
 arr[i] = String.valueOf(sc.nextInt());
 }



printLargest(Arrays.asList(arr));
 }


 static void
printLargest(List<String> arr){
 Collections.sort(arr, new
Comparator<String>(){
 // A comparison function which is used by

// sort() in printLargest() @Override public int
compare(String X, String Y) {
 // first append Y at the end of X

String XY=X + Y;
 // then append X
 at the end of Y
String YX=Y + X;
 // Now see which of the two formed numbers
// is greater return XY.compareTo(YX) > 0 ? -1:1;
 }



}


);
 Iterator it = arr.iterator();
 while(it.hasNext())
System.out.print(it.next());
 }


}




You might also like