0

I have a problem with my loop. This is loop:

for(String element: listOfSBPData){
            date1 = (String) element.subSequence(0,2);
            date2 = (String) tvDateFrom.getText().subSequence(0,2);
            if(element.contains(tvDateFrom.getText())){
                for(int i = 0; i<7; i++)
                if(date1.contains(date2)){
                    listOfSBPDataToPlot.add(element);
                    listOfSBPToPlot.add(listOfSBP.get(element.//here I want to get id my element))
                }
            }   
        }

I have a problem to get Id for "element" in place in code where I put comment. How I can get index of this element? I need index of this element because I have other table and I want to get element with the same index.

3
  • use good old for (int i ... Commented Mar 14, 2013 at 13:54
  • so if I use that way I can't get index of this element? Commented Mar 14, 2013 at 13:57
  • unless you add your own counter, no Commented Mar 14, 2013 at 14:04

1 Answer 1

1
 int index =0;

for(String element: listOfSBPData){
        date1 = (String) element.subSequence(0,2);
        date2 = (String) tvDateFrom.getText().subSequence(0,2);
        if(element.contains(tvDateFrom.getText())){
            for(int i = 0; i<7; i++)
            if(date1.contains(date2)){
                listOfSBPDataToPlot.add(element);
                listOfSBPToPlot.add(listOfSBP.get(index));
            }
        }   
        index++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just be careful if you end up removing or adding things to the list you are iterating over

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.