> I am wondering how i could write a for loop to compare the lengths of multiple lists in an array to find the shortest on

I am wondering how i could write a for loop to compare the lengths of multiple lists in an array to find the shortest on

Posted at: 2014-12-18 
You might try something like this:

x=1,000,000

for i=1,(number of lists to compare) do

if list(i)< x then

x=list(i)

end

end

This sets x to the length of the first list and then compares each list length as it runs through the loop, leaving you with x=the shortest list when done. I intialized x with 1,000,000 because it would be much higher than any possible list length.

Check out the following URL:

http://javacodex.com/Collections/Array-O...

It is an example that creates an array of Arraylists then populates each Arraylists with a random number of entries. Then the size of each ArrayList in the Array is printed out.

public class Program {

public static void main(String[] args) {

String[][] names = {{"one", "two", "three"}, {"one"}, {"one", "two"}};

int min = Integer.MAX_VALUE;

for (int i = 0; i < names.length; i++) {

min = Math.min(min, names[i].length);

}

System.out.format("The shortest path is %d%n", min); // output: 1

}

}