在Java Array中设置布尔值

时间:2012-12-03 05:08:01

标签: java android arrays

这是我非常精心修剪的版本,仅包含布尔值及其周围的结构:

public int payoffDebt(Double totalDebt) {

    boolean booIsPaid[] = new boolean[c.getCount()];
    int bb = 0; 
    int bbb = 0;


    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        booIsPaid[bb++] = false;
    }

    while (totalDebt > 0) {

        for (int i : rowCounter) { 

            if ((indBal[p] <= 0) && (booIsPaid[bbb] == false)) {
                booIsPaid[bbb++] = true;
            } 


        }

    }
    return monthTotal;
}

这就是我想要做的事情,早期我循环用户“debts”并为每个名为booIsPaid的人分配一个布尔值。它们设置为false,这样可以正常工作。但是,在下面,我认为我没有正确地迭代它们,因为在我的Android应用程序中,我得到Window Leaking LogCat在我的Activity中调用它。我评论我将它设置为true的位置,并且一切正常。有人能看到哪里不对吗?

编辑:Stacktrace:

12-02 21:14:35.121: E/AndroidRuntime(8832): FATAL EXCEPTION: AsyncTask #1
12-02 21:14:35.121: E/AndroidRuntime(8832): java.lang.RuntimeException: An error occured while executing doInBackground()
12-02 21:14:35.121: E/AndroidRuntime(8832):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at java.lang.Thread.run(Thread.java:856)
12-02 21:14:35.121: E/AndroidRuntime(8832): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
12-02 21:14:35.121: E/AndroidRuntime(8832):     at com.---.---.DebtDataSource.payoffDebt(DebtDataSource.java:290)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at com.---.---.Planner$PlannerTask.doInBackground(Planner.java:73)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at com.---.---.Planner$PlannerTask.doInBackground(Planner.java:1)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-02 21:14:35.121: E/AndroidRuntime(8832):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-02 21:14:35.121: E/AndroidRuntime(8832):     ... 5 more
12-02 21:14:35.128: W/ActivityManager(291):   Force finishing activity com.---.---/.Planner
12-02 21:14:35.175: W/ActivityManager(291):   Force finishing activity com.---.---/.ManageDebts
12-02 21:14:35.285: W/ActivityManager(291): Duplicate finish request for ActivityRecord{41e0b390 com.---.---/.Planner}
12-02 21:14:35.496: I/ActivityManager(291): Displayed com.---.---/.Planner: +557ms
12-02 21:14:35.605: E/WindowManager(8832): Activity com.---.---.Planner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4175e930 that was originally added here
12-02 21:14:35.605: E/WindowManager(8832): android.view.WindowLeaked: Activity com.---.---.Planner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4175e930 that was originally added here
12-02 21:14:35.605: E/WindowManager(8832):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
12-02 21:14:35.605: E/WindowManager(8832):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
12-02 21:14:35.605: E/WindowManager(8832):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-02 21:14:35.605: E/WindowManager(8832):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-02 21:14:35.605: E/WindowManager(8832):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
12-02 21:14:35.605: E/WindowManager(8832):  at android.app.Dialog.show(Dialog.java:277)
12-02 21:14:35.605: E/WindowManager(8832):  at com.---.---.Planner$PlannerTask.onPreExecute(Planner.java:56)
12-02 21:14:35.605: E/WindowManager(8832):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-02 21:14:35.605: E/WindowManager(8832):  at android.os.AsyncTask.execute(AsyncTask.java:534)
12-02 21:14:35.605: E/WindowManager(8832):  at com.---.---.Planner.onCreate(Planner.java:36)
12-02 21:14:35.605: E/WindowManager(8832):  at android.app.Activity.performCreate(Activity.java:5008)
12-02 21:14:35.605: E/WindowManager(8832):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-02 21:14:35.605: E/WindowManager(8832):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-02 21:14:35.605: E/WindowManager(8832):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-02 21:14:35.605: E/WindowManager(8832):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-02 21:14:35.605: E/WindowManager(8832):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-02 21:14:35.605: E/WindowManager(8832):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 21:14:35.605: E/WindowManager(8832):  at android.os.Looper.loop(Looper.java:137)
12-02 21:14:35.605: E/WindowManager(8832):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-02 21:14:35.605: E/WindowManager(8832):  at java.lang.reflect.Method.invokeNative(Native Method)
12-02 21:14:35.605: E/WindowManager(8832):  at java.lang.reflect.Method.invoke(Method.java:511)
12-02 21:14:35.605: E/WindowManager(8832):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-02 21:14:35.605: E/WindowManager(8832):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-02 21:14:35.605: E/WindowManager(8832):  at dalvik.system.NativeStart.main(Native Method)

修改: 我已经重新设计了所有内容,这是我的新方法:

while (totalDebt > 0) {

booIsPaid = false;
remainingBalance = 0;

// Increment month
monthTotal = monthTotal + 1;

// update INDIVIDUAL debt Here
for (int i : rowCounter) { 

nameList.add(indName[r++]);

// Zero Balance, set Boolean true, add to Remainder     
if ((indBal[p] <= 0) && !booIsPaid) { 
        booIsPaid = true;
        remainingbalance = remainingbalance + indPay[q];
        indPay[q] = 0;
        indBal[p] = 0;
        payList.add(Double.valueOf(IndPay));
    // First balance after zero; set Boolean false, add to remainder to payment.
    } else if  ((indBal[p] > 0) && booIsPaid) { 
       booIsPaid = false;       
       indPay[q] = indPay[q] + remainingbalance;
        indPay[q] = Math.round(indPay[q] * 100.00) / 100.00;
        IndPay = myFormat.format(indPay[q]);
        payList.add(Double.valueOf(IndPay));

 // Other remaining balances
    } else if  ((indBal[p] > 0) && !booIsPaid) {        indPay[q] = 0;
        indPay[q] = Math.round(indPay[q] * 100.00) / 100.00;
        IndPay = myFormat.format(indPay[q]);
        payList.add(Double.valueOf(IndPay));
    }

// indPay[q] (should or should not include remainder here!
    indBal[p] = indBal[p] - (indPay[q] - interestFee);
    totalDebt = totalDebt - (indPay[q++] - interestFee);

    IndBal = myFormat.format(indBal[p++]);
    indBalList.add(Double.valueOf(IndBal));
    feeList.add(Double.valueOf(InterestFee));
}
}

注意:输出存储在此处的四个ArrayLists中,将逐月打印给用户。

整个实际应用也提出了债务偿还策略。我还没有对此进行测试并将报告回来。任何反馈?让我知道!

3 个答案:

答案 0 :(得分:2)

这是你的问题:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.---.---.DebtDataSource.payoffDebt(DebtDataSource.java:290)

您正在尝试访问数组范围之外的索引。查看ArrayIndexOutOfBoundsException文档。

您可以发布此特定行代码:

DebtDataSource.payoffDebt(DebtDataSource.java:290)

您需要检查变量bbb是否在数组范围内。我认为这是问题所在,请尝试将代码更改为:

if ((indBal[p] <= 0) && (booIsPaid[bbb] == false) && (bbb <= booIsPaid.length)){
      booIsPaid[bbb++] = true;
} 

我还建议您在循环代码的某个位置将bbb的值重新设置为零。

希望它有所帮助!

答案 1 :(得分:2)

下面的一些问题/意见:

  1. 不需要第一个循环,因为boolean []已初始化,值为false

  2. 无需与boolean进行false比较,因为您只需撰写if ((indBal[p] <= 0) && !booIsPaid[bbb]) {

  3. 在while循环中没有检查数组长度(导致您的问题)。添加如下:

    while (totalDebt > 0 && bbb <booIsPaid.length) {
      for (int i : rowCounter) { 
        if (indBal[p] <= 0 && !booIsPaid[bbb]  &&  bbb <booIsPaid.length) {
            booIsPaid[bbb++] = true;
        } 
      }
    }
    

答案 2 :(得分:1)

首先,您不必将布尔数组中的每个值都设置为false。它们默认设置为。

其次,你的循环确实看起来有点可疑。以下将解决您正在获得的AIOOBException。

while (totalDebt > 0) {
    for (int i : rowCounter) { 
        if (indBal[p] <= 0 && !booIsPaid[bbb]) {
            booIsPaid[bbb++] = true;
        } 
    }
    bbb = 0;
}
return monthTotal;

原因是:bbbwhilefor进行迭代时无限制地增长。