简体   繁体   中英

ArrayList not working as expected in do..while loop

I have the following loop which creates a String which fits exactly in a screen, it creates a page so to speak.

    for (int i = 0; i < totalLine; i++) {

        temp = enterLine(mTextView, textToBeShown, i);
            full = full + temp;

    }

So after the iteration is done full holds one page.

What i want to do is create an outer iteration which allows me to create more than one page, the amount of pages to be created isn't defined. So the iteration needs to stop if there aren't more pages to be created.

I tried the following code but for some reason when calling a page Pages.get(1) it gives out the whole String not just the full / Page. For Example if i three strings have been added to the ArrayList Pages there will be three Strings in the ArrayList but all with the same value.

With some testing with the Log , i know that the first iteration is working well, and that full gives the expected values meaning in the first do iteration gives out the expected values to the full so does the second iteration etc..

    do{
    for (int i = 0; i < totalLine; i++) {

        temp = enterLine(mTextView, textToBeShown, i);
        if(temp.trim().length() == 0){
            break;
        }else{
            full = full + temp;
        }
    }
    Pages.add(full);

So the question is what am i doing wrong with the ArrayList and why isn't it working as I'm expecting.

Edit

This is the enterLine code: More Log 's were used didn't feel the need the display them all.

public String enterLine(TextView mTextView, String textBeShown, int i){

        String A;
        int number = mTextView.getPaint().breakText(textToBeShown, 0, textToBeShown.length(),true,mTextView.getWidth(), null);

        if(textToBeShown.substring(number-1,number) != " "){
            number = textToBeShown.substring(0,number).lastIndexOf(" ");
            Log.e("TAG1", "Found " + i);
        }

        A = (textToBeShown.substring(0, number) + "\n");
        Log.e(TAG, textToBeShown.substring(0, number));
        textToBeShown = textToBeShown.substring(number, textToBeShown.length());
        return A;
    }

From the looks of it, it's not your arraylist but your loop. Add adds an element to the arraylist, get(index) gets the index'th element from the list. No problem there.

Your problem is that it adds full to the page after the loop, by which point full already contains everything. Put the pages.add inside the loop and it'll be fixed. Make sure you reset full each iteration. Put full = "" at the start of the loop. Should work then.

do{
    full=""
    for (int i = 0; i < totalLine; i++) {
        temp = enterLine(mTextView, textToBeShown, i);
        if(temp.trim().length() == 0){
            break;
        }else{
            full = full + temp;
        }
    }
    Pages.add(full);
}while(...)

or better

do{
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < totalLine; i++) {
        temp = enterLine(mTextView, textToBeShown, i));
        if(temp.trim().length() == 0){
            break;
        }else{
            builder.append(temp);
        }
    }
    Pages.add(builder.toString());
}while(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM