1

Below is the List Class.

public class Abc {

    String pincode, state, name, phNo;

    public Abc(String pincode, String state, String name, String phNo) {
        this.pincode = pincode;
        this.state = state;
        this.name = name;
        this.phNo = phNo;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhNo() {
        return phNo;
    }

    public void setPhNo(String phNo) {
        this.phNo = phNo;
    }
}

I am adding the data to the list.

    List<Abc> abcs = new ArrayList<>();
    List<Abc> customList = new ArrayList<>();
    abcs.add(new Abc("600025", "Delhi", "gambhir", "565632552"));
    abcs.add(new Abc("600026", "Punjab", "yuvi", "565632553"));
    abcs.add(new Abc("600027", "Punjab", "bhajji", "565632554"));
    abcs.add(new Abc("600028", "Delhi", "kohli", "565632555"));
    abcs.add(new Abc("600029", "Karnataka", "dravid", "565632556"));
    abcs.add(new Abc("600030", "Delhi", "rohit", "565632557"));
    abcs.add(new Abc("600031", "Kerla", "sreeshanth", "565632558"));

I need a separate list who lives in Delhi how to achieve this logic. I tried like this

for (int i = 0; i < abcs.size(); i++) {
      if(abcs.get(i).getName().equals("Delhi")){
            customList.addAll(i,abcs);
      }
    }

But in my customList no data is added. Can anyone help me.

3
  • try putting the Abc class object in your customList. customList.add(abcs.get(i)); Commented Sep 16, 2016 at 6:32
  • wait.. I will check.. Commented Sep 16, 2016 at 6:33
  • This condition if(abcs.get(i).getName().equals("Delhi")){ should be if(abcs.get(i).getState().equals("Delhi")){ Commented Sep 16, 2016 at 6:33

3 Answers 3

3

Again change code to

for (int i = 0; i < abcs.size(); i++) {
        if (abcs.get(i).getState().equals("Delhi")) {
            customList.add(abcs.get(i));
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

This condition

if(abcs.get(i).getName().equals("Delhi")){

should be

if(abcs.get(i).getState().equals("Delhi")){

because you'r storing state name in second parameter and in "name" variable there is not value for "Delhi"

Comments

1

I think you can create a custom model for storing info about only delhi users.

public class CustomList{
private ArrayList<Abc> abc;
public CustomList(ArrayList<Abc> abc){
this.abc = abc;
}
public void setAbc(ArrayList<Abc> abc){
this.abc = abc;
}

public ArrayList<Abc> getAbc(){
return abc;
}
}

Now, you can add your code and check for Delhi users:

ArrayLis<CustomList> customList = new ArrayList<CustomList>();
for (int i = 0; i < abcs.size(); i++) {
      if(abcs.get(i).getState().equals("Delhi")){
            customList.add(new CustomList(abcs));
      }
    }

retrieve by: customList.get(position).getAbc();

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.