Java 有没有一种方法可以包含i+;1在广播类函数内的for循环中?

Java 有没有一种方法可以包含i+;1在广播类函数内的for循环中?,java,android,arrays,parsing,broadcastreceiver,Java,Android,Arrays,Parsing,Broadcastreceiver,我试图循环遍历包含数字的字符串数组,并将这些值解析为整数集合,将返回值显示在广播中,例如“a work from---,returned value” 我已使用.split(',')将字符串拆分为一个字符串数组,并在将字符串值解析为整数时尝试循环遍历该数组。 如果我可以直接输入索引parseInt(FilterDataValues[1])或任何索引,我可以从for循环中获取返回值,但是一旦我添加IparseInt(FilterDataValues[I+1])或parseInt(FilterDat

我试图循环遍历包含数字的字符串数组,并将这些值解析为整数集合,将返回值显示在广播中,例如“a work from---,returned value”

我已使用
.split(',')
将字符串拆分为一个字符串数组,并在将字符串值解析为整数时尝试循环遍历该数组。 如果我可以直接输入索引
parseInt(FilterDataValues[1])
或任何索引,我可以从for循环中获取返回值,但是一旦我添加I
parseInt(FilterDataValues[I+1])
parseInt(FilterDataValues[I])
我就无法获取返回值。这个循环在一个更简单的循环中运行良好,所以我不太确定发生了什么。请帮助我,任何帮助都可以

 public String sortingList(Context context)
    {
        String filterData = "";
        StimulusDatabase td = new StimulusDatabase(context);

        Cursor result = td.getAllData();
        while (result.moveToNext())
        {
            filterData = (result.getString(23));
        }

        if(filterData.isEmpty())
        {
            return "";
        }
        else
        {
            String[] FilterDataValues = filterData.split(",");

            for(int i = 0; i <= FilterDataValues.length; i++)
            {
                FilterOptions.add(parseInt(FilterDataValues[i+1]));
            }

//            for(int i = 0; i <= FilterDataValues.length; i++)
//            {
//                FilterOptions.add(i+1));
//            }

            return filterData;
        }
    }
全班同学都在下面

package com.example.MentalHealthStateManagement.Application.Notifications;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.util.Log;

import com.example.MentalHealthStateManagement.Application.Database.StimulusDatabase;
import com.example.MentalHealthStateManagement.R;
import com.example.MentalHealthStateManagement.StateHandler.CheckIn;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import androidx.core.app.NotificationManagerCompat;

import static java.lang.Integer.parseInt;

public class Trigger_Receiver extends BroadcastReceiver
{
    private static final String CHANNEL_ID = "M.H.S.M daily check-in registry";
    private static final String TAG = "Trigger Receiver: ";

    private String[] authors;
    private String sortStatement;
    private List<Integer> FilterOptions = new ArrayList<>();
    private int notificationID = 135;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        sortStatement = sortingList(context);
        authors = getAuthors(context).split("#");
        String[] quotes = getQuotes(context).split("#");

        int randomNum = randomPosition();

        // Create an exclusive intent for the user's daily check-in
        intent = new Intent(context, CheckIn.class);

        // Create the TaskStack Builder and add the Main Activity, which inflates the back stack
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntentWithParentStack(intent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // Checks if the API Level is 26 or greater
        Notification.Builder notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notification = new Notification.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                    .setContentTitle("A word from " + authors[randomNum])
                    .setContentText(sortingList(context))//quotes[randomNum]
                    .setStyle(new Notification.BigTextStyle().bigText(sortingList(context)))
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);
        }

        NotificationManagerCompat notificationManger = NotificationManagerCompat.from(context);
        assert notification != null;
        notificationManger.notify(notificationID, notification.build());
        notificationID++;
        Log.i("Trigger_Receiver", "Trigger successful");
    }

    //Gets the authors for the specified trigger
    public String getAuthors(Context context)
    {
        StringBuilder authors = new StringBuilder();
        StimulusDatabase td = new StimulusDatabase(context);
       // List<Integer> AuthorsToBeFiltered = NumberFilterValue;

        Cursor result = td.getAllData();

            while (result.moveToNext())
            {
                authors = new StringBuilder(result.getString(2) + " " + result.getString(4) + " "
                        + result.getString(6) + " " + result.getString(8) + " " +
                            result.getString(10) + " " + result.getString(12) + " "
                        + result.getString(14) + " " + result.getString(16) + " " +
                            result.getString(18) + " " + result.getString(20));
            }

//            while (result.moveToNext())
//            {
//                if(!AuthorsToBeFiltered.isEmpty())
//                {
//                    for(int i = 0; i<AuthorsToBeFiltered.size(); i++)
//                    {
//                        if(AuthorsToBeFiltered.get(i) % 2 == 0)
//                        {
//                            authors.append(result.getString(i));
//                        }
//                    }
//                }
//                else
//                {
//                    authors = new StringBuilder(result.getString(2) + " " + result.getString(4) + " " + result.getString(6) + " " + result.getString(8) + " " +
//                            result.getString(10) + " " + result.getString(12) + " " + result.getString(14) + " " + result.getString(16) + " " +
//                            result.getString(18) + " " + result.getString(20));
//                }
//            }

        return authors.toString();
    }

    //Gets the quotes for the specified trigger
    public String getQuotes(Context context)
    {
        StringBuilder quotes = new StringBuilder();
        StimulusDatabase td = new StimulusDatabase(context);
        //List<Integer> AuthorsToBeFiltered = NumberFilterValue;

        Cursor result = td.getAllData();

                while (result.moveToNext())
                {
                    quotes.append(result.getString(1)).append(" ").append(result.getString(3))
                            .append(" ").append(result.getString(5)).append(" ").append(result.getString(7))
                            .append(" ").append(result.getString(9)).append(" ").append(result.getString(11))
                            .append(" ").append(result.getString(13)).append(" ").append(result.getString(15))
                            .append(" ").append(result.getString(17)).append(" ").append(result.getString(19));
                }

//            while (result.moveToNext())
//            {
//                if(!AuthorsToBeFiltered.isEmpty())
//                {
//                    for(int i = 0; i<AuthorsToBeFiltered.size(); i++)
//                    {
//                        if(AuthorsToBeFiltered.get(i) % 2 == 0)
//                        {
//                            quotes.append(result.getString(i-1));
//                        }
//                    }
//                }
//                else
//                {
//                    quotes.append(result.getString(1)).append(" ").append(result.getString(3))
//                            .append(" ").append(result.getString(5)).append(" ").append(result.getString(7))
//                            .append(" ").append(result.getString(9)).append(" ").append(result.getString(11))
//                            .append(" ").append(result.getString(13)).append(" ").append(result.getString(15))
//                            .append(" ").append(result.getString(17)).append(" ").append(result.getString(19));
//                }
//            }

        return quotes.toString();
    }

    //A random number used to gather different quotes and authors
    //P.S. each quote belongs to appropriate author due to the delimiter settings preformed in the StimulusSetup class
    public Integer randomPosition()
    {
        int randomNum = 0;
        // Ensures the API level 26 or greater
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            randomNum = ThreadLocalRandom.current().nextInt(0, authors.length + 1);
        }
        else
        {
            Log.d(TAG,"Incompatible device, API lower than lollipop");
        }
        return randomNum;
    }

    public String sortingList(Context context)
    {
        String filterData = "";
        StimulusDatabase td = new StimulusDatabase(context);

        Cursor result = td.getAllData();
        while (result.moveToNext())
        {
            filterData = (result.getString(23));
        }

        if(filterData.isEmpty())
        {
            return "";
        }
        else
        {
            String[] FilterDataValues = filterData.split(",");

            for(int i = 0; i <= FilterDataValues.length; i++)
            {
                FilterOptions.add(parseInt(FilterDataValues[i+1]));
            }

//  `         Working loop
//            for(int i = 0; i <= FilterDataValues.length; i++)
//            {
//                FilterOptions.add(i+1));
//            }

            return filterData;
        }
    }
}
package com.example.MentalHealthStateManagement.Application.Notifications;
导入android.app.Notification;
导入android.app.pendingent;
导入android.app.TaskStackBuilder;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.database.Cursor;
导入android.os.Build;
导入android.util.Log;
导入com.example.MentalHealthStateManagement.Application.Database.StimulusDatabase;
导入com.example.MentalHealthStateManagement.R;
导入com.example.MentalHealthStateManagement.StateHandler.CheckIn;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.concurrent.ThreadLocalRandom;
导入androidx.core.app.NotificationManagerCompat;
导入静态java.lang.Integer.parseInt;
公共类触发器接收器扩展广播接收器
{
专用静态最终字符串通道\u ID=“M.H.S.M每日值机登记”;
私有静态最终字符串标记=“触发器接收器:”;
私有字符串[]作者;
私有字符串排序语句;
私有列表筛选器选项=新的ArrayList();
私有int notificationID=135;
@凌驾
公共void onReceive(上下文、意图)
{
sortStatement=排序列表(上下文);
authors=getAuthors(context).split(“#”);
String[]quotes=getQuotes(context.split(“#”);
int randomNum=randomPosition();
//为用户的每日入住创建专属目的
intent=新的intent(上下文,CheckIn.class);
//创建TaskStack Builder并添加主活动,这会使后堆栈膨胀
TaskStackBuilder stackBuilder=TaskStackBuilder.create(上下文);
stackBuilder.AddNextEntWithParentStack(意图);
PendingEvent PendingEvent=stackBuilder.GetPendingEvent(0,PendingEvent.FLAG_UPDATE_CURRENT);
//检查API级别是否为26或更高
Notification.Builder Notification=null;
if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u code.O){
通知=新建通知.Builder(上下文,通道ID)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle(“来自”+作者[randomNum]的单词)
.setContentText(排序列表(上下文))//引号[randomNum]
.setStyle(新通知.BigTextStyle().bigText(排序列表(上下文)))
.setContentIntent(挂起内容)
.setAutoCancel(真);
}
NotificationManagerCompat NotificationManager=NotificationManagerCompat.from(上下文);
断言通知!=null;
notificationmanager.notify(notificationID,notification.build());
notificationID++;
Log.i(“触发接收器”,“触发成功”);
}
//获取指定触发器的作者
公共字符串getAuthors(上下文)
{
StringBuilder authors=新建StringBuilder();
StimulusDatabase td=新的StimulusDatabase(上下文);
//列表作者ToBefilted=NumberFilterValue;
游标结果=td.getAllData();
while(result.moveToNext())
{
authors=新建StringBuilder(result.getString(2)+“”+result.getString(4)+“”
+result.getString(6)+“”+result.getString(8)+“”+
result.getString(10)+“result.getString(12)+”
+result.getString(14)+“”+result.getString(16)+“”+
result.getString(18)+“”+result.getString(20));
}
//while(result.moveToNext())
//            {
//如果(!AuthorsToBeFiltered.isEmpty())
//                {

//对于(inti=0;i注意,在循环中,您正在调用

FilterOptions.add(parseInt(FilterDataValues[i+1]);

在循环的最后一次迭代中应该失败,因为将尝试从该数组中不存在的位置读取值。我猜您可以在LogCat中找到
IndexOutOfBoundException


这可能是您无法获取返回值的原因,因为方法应该抛出异常。

注意,在循环中,您正在调用

FilterOptions.add(parseInt(FilterDataValues[i+1]);

在循环的最后一次迭代中应该失败,因为将尝试从该数组中不存在的位置读取值。我猜您可以在LogCat中找到
IndexOutOfBoundException


这可能是您无法获得返回值的原因,因为方法应该抛出异常。

我理解为什么我会获得
IndexOutOfBoundException
,因为即使是最后一个索引,我也会尝试执行上面的步骤,但如果没有
I+1
,仅使用索引
I
,则不会显示返回值。这就是这是我当前的问题如果在OP中没有说明,我道歉我理解为什么我会得到一个
IndexOutOfBoundException
,因为我最终会在索引中
package com.example.MentalHealthStateManagement.Application.Notifications;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.util.Log;

import com.example.MentalHealthStateManagement.Application.Database.StimulusDatabase;
import com.example.MentalHealthStateManagement.R;
import com.example.MentalHealthStateManagement.StateHandler.CheckIn;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import androidx.core.app.NotificationManagerCompat;

import static java.lang.Integer.parseInt;

public class Trigger_Receiver extends BroadcastReceiver
{
    private static final String CHANNEL_ID = "M.H.S.M daily check-in registry";
    private static final String TAG = "Trigger Receiver: ";

    private String[] authors;
    private String sortStatement;
    private List<Integer> FilterOptions = new ArrayList<>();
    private int notificationID = 135;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        sortStatement = sortingList(context);
        authors = getAuthors(context).split("#");
        String[] quotes = getQuotes(context).split("#");

        int randomNum = randomPosition();

        // Create an exclusive intent for the user's daily check-in
        intent = new Intent(context, CheckIn.class);

        // Create the TaskStack Builder and add the Main Activity, which inflates the back stack
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntentWithParentStack(intent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // Checks if the API Level is 26 or greater
        Notification.Builder notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notification = new Notification.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                    .setContentTitle("A word from " + authors[randomNum])
                    .setContentText(sortingList(context))//quotes[randomNum]
                    .setStyle(new Notification.BigTextStyle().bigText(sortingList(context)))
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);
        }

        NotificationManagerCompat notificationManger = NotificationManagerCompat.from(context);
        assert notification != null;
        notificationManger.notify(notificationID, notification.build());
        notificationID++;
        Log.i("Trigger_Receiver", "Trigger successful");
    }

    //Gets the authors for the specified trigger
    public String getAuthors(Context context)
    {
        StringBuilder authors = new StringBuilder();
        StimulusDatabase td = new StimulusDatabase(context);
       // List<Integer> AuthorsToBeFiltered = NumberFilterValue;

        Cursor result = td.getAllData();

            while (result.moveToNext())
            {
                authors = new StringBuilder(result.getString(2) + " " + result.getString(4) + " "
                        + result.getString(6) + " " + result.getString(8) + " " +
                            result.getString(10) + " " + result.getString(12) + " "
                        + result.getString(14) + " " + result.getString(16) + " " +
                            result.getString(18) + " " + result.getString(20));
            }

//            while (result.moveToNext())
//            {
//                if(!AuthorsToBeFiltered.isEmpty())
//                {
//                    for(int i = 0; i<AuthorsToBeFiltered.size(); i++)
//                    {
//                        if(AuthorsToBeFiltered.get(i) % 2 == 0)
//                        {
//                            authors.append(result.getString(i));
//                        }
//                    }
//                }
//                else
//                {
//                    authors = new StringBuilder(result.getString(2) + " " + result.getString(4) + " " + result.getString(6) + " " + result.getString(8) + " " +
//                            result.getString(10) + " " + result.getString(12) + " " + result.getString(14) + " " + result.getString(16) + " " +
//                            result.getString(18) + " " + result.getString(20));
//                }
//            }

        return authors.toString();
    }

    //Gets the quotes for the specified trigger
    public String getQuotes(Context context)
    {
        StringBuilder quotes = new StringBuilder();
        StimulusDatabase td = new StimulusDatabase(context);
        //List<Integer> AuthorsToBeFiltered = NumberFilterValue;

        Cursor result = td.getAllData();

                while (result.moveToNext())
                {
                    quotes.append(result.getString(1)).append(" ").append(result.getString(3))
                            .append(" ").append(result.getString(5)).append(" ").append(result.getString(7))
                            .append(" ").append(result.getString(9)).append(" ").append(result.getString(11))
                            .append(" ").append(result.getString(13)).append(" ").append(result.getString(15))
                            .append(" ").append(result.getString(17)).append(" ").append(result.getString(19));
                }

//            while (result.moveToNext())
//            {
//                if(!AuthorsToBeFiltered.isEmpty())
//                {
//                    for(int i = 0; i<AuthorsToBeFiltered.size(); i++)
//                    {
//                        if(AuthorsToBeFiltered.get(i) % 2 == 0)
//                        {
//                            quotes.append(result.getString(i-1));
//                        }
//                    }
//                }
//                else
//                {
//                    quotes.append(result.getString(1)).append(" ").append(result.getString(3))
//                            .append(" ").append(result.getString(5)).append(" ").append(result.getString(7))
//                            .append(" ").append(result.getString(9)).append(" ").append(result.getString(11))
//                            .append(" ").append(result.getString(13)).append(" ").append(result.getString(15))
//                            .append(" ").append(result.getString(17)).append(" ").append(result.getString(19));
//                }
//            }

        return quotes.toString();
    }

    //A random number used to gather different quotes and authors
    //P.S. each quote belongs to appropriate author due to the delimiter settings preformed in the StimulusSetup class
    public Integer randomPosition()
    {
        int randomNum = 0;
        // Ensures the API level 26 or greater
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            randomNum = ThreadLocalRandom.current().nextInt(0, authors.length + 1);
        }
        else
        {
            Log.d(TAG,"Incompatible device, API lower than lollipop");
        }
        return randomNum;
    }

    public String sortingList(Context context)
    {
        String filterData = "";
        StimulusDatabase td = new StimulusDatabase(context);

        Cursor result = td.getAllData();
        while (result.moveToNext())
        {
            filterData = (result.getString(23));
        }

        if(filterData.isEmpty())
        {
            return "";
        }
        else
        {
            String[] FilterDataValues = filterData.split(",");

            for(int i = 0; i <= FilterDataValues.length; i++)
            {
                FilterOptions.add(parseInt(FilterDataValues[i+1]));
            }

//  `         Working loop
//            for(int i = 0; i <= FilterDataValues.length; i++)
//            {
//                FilterOptions.add(i+1));
//            }

            return filterData;
        }
    }
}