Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

sort array based on string length java

import java.util.*;
class Main
{
    public static void main(String[] args) 
    {
        ArrayList<String> arr = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        sc.nextLine();
        
        for(int i=0; i<n; i++){
            arr.add(sc.nextLine());
        }
        
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if((arr.get(i)).length() > (arr.get(j)).length()){
                    Collections.swap(arr,i,j);
                }
            }
        }
        
        System.out.println(arr);
        
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: How to efficiently find three largest numbers of array, in Java? 
Java :: how to create an array in java 
Java :: Error: Could not find or load main class -Djava.library.path=.home.hdoop.hadoop-3.3.0.lib.native 
Java :: android activity read intent extra 
Java :: java import text file into arraylist 
Java :: java generate random id 
Java :: what it means when create final variable in java 
Java :: how to convert string to boolean in java 
Java :: jframe set visibility 
Java :: java selenium send keys numbers 
Java :: how to control clip volume java 
Java :: java change text on click 
Java :: java get year from date 
Java :: 2d arraylist in java 
Java :: get last index of array java 
Java :: public String toString() { 
Java :: java return list 
Java :: remove fragment from backstack 
Java :: volatile in java 
Java :: how to read a excel file in java 
Java :: why bufferedreader is faster than scanner 
Java :: 12 hours to 24 hours java 
Java :: string to string array java 
Java :: play mp3 android java 
Java :: slicing array in java 
Java :: java opts 
Java :: check if sqlexception is duplicate entry java 
Java :: what does static mean java 
Java :: loop through an arraylist android studio 
Java :: Add delay to code in Android 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =