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 :: first and last digit of a number in java 
Java :: java time 
Java :: math.min java 
Java :: get key from value hashmap 
Java :: java clear string buffer 
Java :: H2 enabling remote database creation first 
Java :: java to the power of 
Java :: imageview.setbackground 
Java :: is java compiled or interpreted 
Java :: java rename file 
Java :: logging in java 
Java :: add a value to a list java in java hashmap 
Java :: sort a list with custom comparator java 
Java :: java netbeans textfield only numbers 
Java :: infinite for loop java example 
Java :: string.indexof java 
Java :: filter and map multiple fields from java stream 
Java :: haxe interface 
Java :: java array sorting java8 
Java :: Caused by: java.io.EOFException: SSL peer shut down incorrectly 
Java :: print a list java 
Java :: what are method in java 
Java :: prime numbers program in java 
Java :: launch java batch 
Java :: vue composition watch 
Java :: int x = 3 & 5 
Java :: Fix arabic javafx 
Java :: Implementation of LinkedHashMap Class in Java map 
Java :: nextint java 
Java :: java mostrar resultados ventana grafica 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =