ArrayList.isEmpty() 時如何返回值? (How do I return a value when ArrayList.isEmpty()?)


問題描述

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 AStultuskeFelix GerberTim BiegeleisenThisaruG)

參考文件

  1. How do I return a value when ArrayList.isEmpty()? (CC BY‑SA 2.5/3.0/4.0)

#is-empty #java #return #return-value #arraylist






相關問題

函數中的變量無法訪問 (Variable in Function not reachable)

如何在 JavaScript 中檢查空/未定義/空字符串? (How can I check for an empty/undefined/null string in JavaScript?)

ember.js 和 webservice / boolean false 作為空字段 (ember.js and webservice / boolean false taken as an empty field)

R:刪除字符變量的多個空列 (R: Remove multiple empty columns of character variables)

檢查 [] simulink (Check for [] simulink)

ArrayList.isEmpty() 時如何返回值? (How do I return a value when ArrayList.isEmpty()?)

如果數組中的一個或多個值為空,Angularjs 會顯示驗證 (Angularjs show validation if one or more value is empty in the array)

XSLT 用於將空正文轉換為 SUCCESS 消息 (XSLT for converting empty body to a SUCCESS message)

EditText getText() 總是返回空字符串 (EditText getText() always returns empty string)

為什麼 FTP 下載無法使用 Java 正常工作 (Why does the FTP download not work properly with Java)

java Splitter 結果列表是否會為空? (Will the java Splitter result list ever be empty?)

如何在我的輸出中包含返回空白值的 grep 搜索? (How to include grep searches that return blank values in my output?)







留言討論