Finding Largest String in ArrayList
package com.java.exp;
import java.util.ArrayList;
public class HighestCharinString {
public static void main(String[] args) {
ArrayList<String> al=new ArrayList<>();
al.add("Bangalore");
al.add("hyd");
al.add("jagan");
al.add("upenderathota");
//I would set your largestString variable to your first String that you add:
int largeststring=al.get(0).length();
int index=0;
for(int i=0;i<al.size();i++)
{
//Then you should use the following to check for the largest String:
if(al.get(i).length()>largeststring)
{
largeststring=al.size();
index=i;
}
}
System.out.println("Index " + index + " "+ al.get(index) + " " + "is the largest and is size " + largeststring);
}
}
//OUTPUT:Index 3 upenderathota is the largest
package com.java.exp;
import java.util.ArrayList;
public class HighestCharinString {
public static void main(String[] args) {
ArrayList<String> al=new ArrayList<>();
al.add("Bangalore");
al.add("hyd");
al.add("jagan");
al.add("upenderathota");
//I would set your largestString variable to your first String that you add:
int largeststring=al.get(0).length();
int index=0;
for(int i=0;i<al.size();i++)
{
//Then you should use the following to check for the largest String:
if(al.get(i).length()>largeststring)
{
largeststring=al.size();
index=i;
}
}
System.out.println("Index " + index + " "+ al.get(index) + " " + "is the largest and is size " + largeststring);
}
}
//OUTPUT:Index 3 upenderathota is the largest