Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java get longest string in array

int index = 0; 
int elementLength = array[0].length();
for(int i=1; i< array.length(); i++) {
    if(array[i].length() > elementLength) {
        index = i; elementLength = array[i].length();
    }
}
return array[index];
Comment

java find longest string in list

// Find longest String in a List
public void changeList(List<String> list) {
    String longestString = list.stream().max(Comparator.comparingInt(String::length)).get();
}
Comment

Java Longest String In String Array

public class JavaLongestStringInStringArray {

  public static String getLongestString(String[] array) {
      int maxLength = 0;
      String longestString = null;
      for (String s : array) {
          if (s.length() > maxLength) {
              maxLength = s.length();
              longestString = s;
          }
      }
      return longestString;
  }

  public static void main(String[] args) {
      String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};
      String longestString = getLongestString(toppings);
      System.out.format("longest string: '%s'
", longestString);
  }

}
Comment

PREVIOUS NEXT
Code Example
Java :: Java Hashmap Access Elements 
Java :: how to write a json file using java 
Java :: java localtime format 
Java :: solid principles in programming 
Java :: event api spigot 
Java :: how to reverse a string in java 
Java :: how to find the largest number in a 2d array java 
Java :: kotlin string interpolation 
Java :: how to create a circle in java 
Java :: convert int to string java 
Java :: android activity read intent extra 
Java :: java random unique number 
Java :: java foreach 
Java :: make frame visible java 
Java :: key listener java 
Java :: change button text java 
Java :: install java debian 8 
Java :: n queens problem leetcode 
Java :: spigot send player action bar message 1.8 
Java :: input char java 
Java :: how to write deserlizer java 
Java :: hide elements android 
Java :: Why should we mention a method throws some exception/s? 
Java :: java random array 
Java :: How to count the number of islands (groups of adjacent 1s) in a grid of 1s (land) and 0s (water), in Java? 
Java :: java printf trailing whitespace 
Java :: iterate map in java 8 using stream 
Java :: awk print second 
Java :: build tools java 
Java :: java remove character from string after 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =