1

I want to get indexOf a first name from ArrayList.

Here is my bean class

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;
}

and here is Main Activity

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"));
    }
}
1
  • To do it the way you want, the ArrayList should have String as generic argument. You'll have to build you own function that iterates over the list, checking object by object if firstname is the one you passed as argument. Commented Mar 10, 2016 at 11:55

4 Answers 4

2

if you want to get index of following name you want to declare a variable

int index = 0;

and replace this line System.out.println(arraylist.indexOf("kshitij")); with this code

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

its return index of your first name where it store. i assume index start from 0

Sign up to request clarification or add additional context in comments.

4 Comments

You need to break, when you find the match
yes we can break but it's print index when the condition is true otherwise is skip to print it
yes we can break but it's print index when the condition is true otherwise is skip to print it, Your statement doesn't make anysense..
its get index only when its condition true (bean.firstname.equals("kshitij")) else its not print index
1

As mentioned here:Java ArrayList IndexOf - Finding Object Index

You should build your own equals method to be able to use indexOf. Also, you will need to pass an object of BeanClass to 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 );
}

Comments

1

Note that your ArrayList contains BeanClass. It's value is not "kshitij", but one of variable of your BeanClass.

You have to declare an index variable, loop ArrayList through and find your index out:

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

Output:

1

By the way, in the BeanClass your variable firstname should be not static, or all of instances will have the firstname same is then searching it by a firstname loses a sence.

So redefine your BeanClass to:

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

   // ...
}

3 Comments

You need to break, when you find the match
Right, no need to loop more. Thanks for a hint.
Also, OP is looking for index, not the value.
1

You can use an IntStream in Java 8 as follows:

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

Remember, getAsInt throws a NoSuchElementException if a matching element is not contained in the list. Don't forget to catch it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.