I have an array of Strings and I want to fill another array with the largest Strings of the aforementioned array. I can get the size of the array correctly and fill it with the correct amount of variables, but two of the variables are null while the third is the correct value.
public static void main(String[] args) {
String[] inputArray = {"aba", "aa", "ad", "vcd", "123"};
String[] resultsArray = allLongestStrings(inputArray);
for(String x: inputArray) {
System.out.println("Input array: " + x);
}
for(String temp: resultsArray) {
System.out.println("Results array: " + temp);
}
}
public static String[] allLongestStrings(String[] inputArray) {
int len = 0;
for(String temp: inputArray) {
if(temp.length() > len) {
len = temp.length();
}
}
String[] ret = new String[len];
for(int i = 0; i <= inputArray.length-1; i++) {
if(inputArray[i].length() == len) {
ret[ret.length-1] = inputArray[i];
}
}
return ret;
}
my results are:
Input array: aba
Input array: aa
Input array: ad
Input array: vcd
Input array: 123
Results array: null
Results array: null
Results array: 123
How can I get the two null values to become aba and vcd?