問題描述
ArrayList.isEmpty() 時如何返回值? (How do I return a value when ArrayList.isEmpty()?)
除了“isEmpty”返回語句外,它都正確打印。如何讓它正確返回“‑1”,以便主函數中的最後 2 個語句完成它們的工作。 注意:我無法編輯主函數
我的代碼是如下:
import java.util.*;
public class task7{
public static int find_minimum_length(ArrayList<String> A)
{
int position = 0;
int smallest = A.get(0).length();
for(int i = 0; i<A.size(); i++)
{
if(A.isEmpty())
{
return ‑1;
}
if(A.get(i).length()<smallest)
{
smallest = A.get(i).length();
int shortt = A.indexOf(A.get(i));
position = shortt;
}
}
return position;
}
public static ArrayList<String> remove_minimum_length(ArrayList<String> A)
{
if(A.isEmpty())
{
}
else
{
A.remove(find_minimum_length(A));
}
return A;
}
public static void main(String[] args)
{
ArrayList<String> a = new ArrayList<String>();
a.add("whale");
a.add("cat");
a.add("elephant");
a.add("donkey");
a.add("goat");
System.out.println(a);
int position = find_minimum_length(a);
System.out.printf("minimum position = %d\n\n", position);
remove_minimum_length(a);
System.out.println(a);
position = find_minimum_length(a);
System.out.printf("minimum position = %d\n\n", position);
remove_minimum_length(a);
System.out.println(a);
position = find_minimum_length(a);
System.out.printf("minimum position = %d\n\n", position);
remove_minimum_length(a);
System.out.println(a);
position = find_minimum_length(a);
System.out.printf("minimum position = %d\n\n", position);
remove_minimum_length(a);
System.out.println(a);
position = find_minimum_length(a);
System.out.printf("minimum position = %d\n\n", position);
remove_minimum_length(a);
System.out.println(a);
position = find_minimum_length(a);
System.out.printf("minimum position = %d\n\n", position);
}
}
參考解法
方法 1:
Move this:
if(A.isEmpty())
{
return ‑1;
}
out of your for‑block. The for block will only be executed if there are elements to iterate over, meaning, if the list is not empty.
方法 2:
Your Problem is that the for
block will be executed as often as the array is big. So if your array is empty the size is 0. So the for
isn't executed.
To fix your Problem you have to move your if
‑clause out of the for
.
Applied to your code it should look like this:
public static int find_minimum_length(ArrayList<String> A)
{
int position = 0;
int smallest = A.get(0).length();
if(!A.isEmpty())
{
for(int i = 0; i<A.size(); i++)
{
if(A.get(i).length()<smallest)
{
smallest = A.get(i).length();
int shortt = A.indexOf(A.get(i));
position = shortt;
}
return position;
}
else
{
return ‑1;
}
}
方法 3:
I would make a few changes to your method. First, you should check for a null
input, and return ‑1
if you find it. You can also return ‑1
in the event of an empty input ArrayList
.
public static int find_minimum_length(List<String> a) {
if (a == null || a.size() == 0) {
return ‑1;
}
int position = 0;
int smallest = a.get(0).length();
// start iterating your for loop at 1, not 0
for (int i=1; i < a.size(); i++) {
if (a.get(i).length() < smallest) {
smallest = a.get(i).length();
position = i;
}
}
return position;
}
public static List<String> remove_minimum_length(List<String> a) {
int index = find_minimum_length(a);
if (index != ‑1) {
a.remove(index);
}
return a;
}
方法 4:
Put your isEmpty()
call outside the for loop.
if(A.isEmpty()) return ‑1;
for(int i = 0; i<A.size(); i++)
{
if(A.get(i).length()<smallest)
{
smallest = A.get(i).length();
int shortt = A.indexOf(A.get(i));
position = shortt;
}
}
(by Daniel O A、Stultuske、Felix Gerber、Tim Biegeleisen、ThisaruG)