java中ArrayList中的循环对象

java中ArrayList中的循环对象,java,Java,我有一个类预算(包含标题和日期属性)和一个包含此数组对象的数组。我想遍历每个对象以找到匹配的字符串。每次我运行这个,都找不到它。先谢谢你 ArrayList<Budget> myArrayList = new ArrayList<Budget>(); public void removeBudget() { String title; int indexNumber; System.out.println("Enter the Title to b

我有一个类预算(包含标题和日期属性)和一个包含此数组对象的数组。我想遍历每个对象以找到匹配的字符串。每次我运行这个,都找不到它。先谢谢你

ArrayList<Budget> myArrayList = new ArrayList<Budget>();
public void removeBudget()
{
    String title;
    int indexNumber;
    System.out.println("Enter the Title to be removed");
    title = scanner.next();
    indexNumber = findId(title);
    if(indexNumber != 0)
    {
        myArrayList.remove(indexNumber);
    }
    else
    {
        System.out.println("Not found");
    }
}
private int findId(String findTitle)
{
    for(int i=0; i<myArrayList.size();i++)
    {
        if(myArrayList.get(i).getTitle().contains(findTitle))
        {
            return i;
        }
    }
    return 0;
}
ArrayList myArrayList=new ArrayList();
公共无效删除预算()
{
字符串标题;
整数指数;
System.out.println(“输入要删除的标题”);
title=scanner.next();
indexNumber=findId(标题);
如果(indexNumber!=0)
{
myArrayList.remove(索引编号);
}
其他的
{
System.out.println(“未找到”);
}
}
私有int findId(字符串findTitle)
{

对于(int i=0;i,正如@Stultuske所说,“你考虑过没有匹配的可能性吗?”以及

源代码

import java.util.ArrayList;
import java.util.List;

public class SO36350049 {
    private class Budget {
        private String title;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }

    public static void main(String[] args) {
        SO36350049 so36350049 = new SO36350049();

        Budget budget1 = so36350049.new Budget();
        budget1.setTitle("Budget 1");

        Budget budget2 = so36350049.new Budget();
        budget2.setTitle("Budget 2");

        List<Budget> budgetList = new ArrayList<Budget>();
        budgetList.add(budget1);
        budgetList.add(budget2);

        int index = findId(budgetList, "Budget 1");
        if(index != -1){
          budgetList.remove(index);
        }
        System.out.println(budgetList.size());
    }

    private static int findId(List<Budget> budgetList, String findTitle) {
        for (int i = 0; i < budgetList.size(); i++) {
            if (budgetList.get(i).getTitle().contains(findTitle)) {
                return i;
            }
        }
        // If you return `0` it'll remove the element at 0th position
        return -1;
    }
}
import java.util.ArrayList;
导入java.util.List;
公共类SO36350049{
私人班级预算{
私有字符串标题;
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
}
公共静态void main(字符串[]args){
SO36350049 SO36350049=新的SO36350049();
预算预算1=so36350049.new Budget();
预算1.设置标题(“预算1”);
预算预算2=so36350049.new Budget();
预算2.设置标题(“预算2”);
List budgetList=新建ArrayList();
budgetList.add(budget1);
预算列表。添加(预算2);
int index=findId(预算列表,“预算1”);
如果(索引!=-1){
预算列表。删除(索引);
}
System.out.println(budgetList.size());
}
私有静态int findId(列表预算列表、字符串findTitle){
对于(int i=0;i
更改调节回路修复了该问题

if(indexNumber >= 0)
    {
        myArrayList.remove(indexNumber);
    }

你有没有考虑过没有匹配的可能性?或者它在索引0处?好吧,这意味着找不到标题-涉及的数据是什么?你有多确定你试图找到的标题确实存在?你有没有调试到代码中?如果你能生成一个索引,那么会更容易帮助。Stultuske关于ind的观点EX 0是一个重要的问题-考虑返回1,如果没有找到条目,而不是0。从返回-1开始作为“未找到”的值,因为0是一个有效的索引,它已经尝试与索引2和3中的对象匹配,并且仍然不工作。,因为,如果在索引0处找到元素,…则将方法中的“未找到”值和之后的检查值都更改为负值。此答案不正确。如果将条件更改为
i
您不会检查列表中的最后一个元素。@Alderath True我想它不应该是-1,因为我正在获取ArrayIndexOutofBoundsExceptionnow@AdarshJayakumar将其更改为-1不会导致ArrayIndexOutofBoundsException。这一定是其他问题。在哪行代码中会出现该异常?@Alderath@Adarsh Jayakumar更正其错误答案我遇到了另一个导致异常的事情,所以我认为您可能面临问题。我的代码中有错误。在创建对象
budget2
后,我在对象
budget1
中设置了值,并且您有正确的循环