【问题标题】:how to get index of an element from bean type array list如何从bean类型数组列表中获取元素的索引
【发布时间】:2016-06-25 06:00:57
【问题描述】:

我想从 ArrayList 中获取 indexOf 一个名字。

这是我的 bean 类

public class BeanClass {
static String firstname;
String lastname;
int rollno;

public BeanClass(String firstname, String lastname, int rollno) {
    super();
    this.firstname = firstname;
    this.lastname = lastname;
    this.rollno = rollno;
}

这里是主要活动

public class MainClass {

    static ArrayList<BeanClass>arraylist=new ArrayList<>();
    static String firstname[]={"kshitij","ravi","prakash","sunil"};
    staticString lastname[]={"singh","sharma","verma","tiwari"};
    staticint rollno[]={1,2,3,4};

    public static void main(String[] args) {

        for (int i = 0; i < firstname.length; i++) {
            BeanClass bean=new BeanClass(firstname[i],lastname[i],rollno[i]);
            arraylist.add(bean);
        }
         System.out.println(arraylist.indexOf("kshitij"));
    }
}

【问题讨论】:

  • 要按照您想要的方式进行操作,ArrayList 应该将 String 作为通用参数。您必须构建自己的函数来遍历列表,如果 firstname 是您作为参数传递的那个,则逐个对象检查。

标签: java arrays list for-loop arraylist


【解决方案1】:

如果你想获得以下名称的索引你想声明一个变量

int index = 0;

并将这一行 System.out.println(arraylist.indexOf("kshitij")); 替换为这段代码

for (BeanClass bean: arraylist) {
    if (bean.firstname.equals("kshitij")) {           
        System.out.println("Index is " + index);
        break;
    }
    index++
}

它存储您的名字的返回索引。我假设索引从 0 开始

【讨论】:

  • 你需要打破,当你找到匹配
  • 是的,我们可以中断,但是当条件为真时它是打印索引,否则跳过打印它
  • yes we can break but it's print index when the condition is true otherwise is skip to print it,你的说法没有任何意义..
  • 只有在条件为真时才获取索引 (bean.firstname.equals("kshitij")) 否则不打印索引
【解决方案2】:

这里提到:Java ArrayList IndexOf - Finding Object Index

您应该构建自己的equals 方法才能使用indexOf。 此外,您需要将BeanClass 的对象传递给indexOf

public boolean equals(Object o) {
    if(!(o instanceof BeanClass)) return false;
    BeanClass other = (BeanClass) o;
    return (this.firstname == other.firstname && this.lastname == other.lastname );
}

【讨论】:

    【解决方案3】:

    请注意,您的 ArrayList 包含 BeanClass。它的值不是"kshitij",而是您的BeanClass 的变量之一。

    你必须声明一个index 变量,循环ArrayList 并找到你的index 输出:

    int index = 0; // Index variable
    for (BeanClass bean: arraylist) {
        if (bean.firstname.equals("kshitij")) {
            System.out.println(index);
            break;
        } 
        index++;
    }
    

    输出:

    1
    

    顺便说一句,在BeanClass 中,您的变量firstname 不应该是static,否则所有实例都将具有firstname,然后按名字搜索它就会失去意义。

    因此,将您的BeanClass 重新定义为:

    public class BeanClass {
       String firstname;
       String lastname;
       int rollno;
    
       // ...
    }
    

    【讨论】:

    • 你需要打破,当你找到匹配
    • 对,不用再循环了。感谢您的提示。
    • 另外,OP 正在寻找索引,而不是值。
    【解决方案4】:

    您可以在 Java 8 中使用IntStream,如下所示:

    IntStream.range(0, list.size())
        .filter(i -> arrayList.get(i).firstname.equals("kshiij"))
        .findFirst()
        .getAsInt();
    

    请记住,如果列表中不包含匹配元素,getAsInt 会抛出 NoSuchElementException。别忘了抓住它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多