简体   繁体   English

ArrayList在do..while循环中未按预期工作

[英]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. 因此,在完成迭代后, full将占据一页。

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. 我尝试了以下代码,但是由于某些原因,在调用页面Pages.get(1)它给出了整个String而不只是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. 例如,如果我三根弦都被添加到ArrayList 页面会出现在三根弦ArrayList ,但都具有相同的价值。

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.. 通过Log一些测试,我知道第一次迭代运行良好,并且full给出了期望值,这意味着在第一次do迭代中给出了期望值到full ,而第二次迭代等等。

    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. 所以问题是我在ArrayList做错了什么,为什么它不能按我期望的那样工作。

Edit 编辑

This is the enterLine code: More Log 's were used didn't feel the need the display them all. 这是enterLine代码:使用了更多Log并不需要全部显示它们。

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. 从外观上看,它不是您的arraylist,而是您的循环。 Add adds an element to the arraylist, get(index) gets the index'th element from the list. Add将一个元素添加到arraylist,get(index)从列表中获取第index个元素。 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. 将pages.add放入循环中,它将得到修复。 Make sure you reset full each iteration. 确保您在每次迭代时都将其重置为完整状态。 Put full = "" at the start of the loop. 在循环的开始处放入full =“”。 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(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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