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 :: list extension quarkus 
Java :: input array through scanner in java 
Java :: hide actionbar android 
Java :: java coding problems 
Java :: java selenium select 
Java :: java sort 2d array 
Java :: how to import java.util 
Java :: how to reverse a list in java 
Java :: javafx every second 
Java :: converter int array para string java 
Java :: java check data type 
Java :: retrofit dependencies in android studio kotlin 
Java :: How to convert timestamp to time in java 
Java :: simple date format 
Java :: sort array java 
Java :: change status bar color android programmatically 
Java :: calculate mean from arraylist jaca 
Java :: java print text 
Java :: setint java 
Java :: startactivityforresult deprecated android 
Java :: java split for multiple characters 
Java :: java log2 
Java :: change height of row at JTable 
Java :: import random java 
Java :: how to open an activity from a fragment 
Java :: iterating over a hashmap 
Java :: print the list in java 
Java :: How to do press enter to continue in java 
Java :: java selenium send keys number 
Java :: convert array to list java 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =