0

我正在做一些火车时刻表应用程序。我已经为他们的相应时间表存储了火车和时间。如果我选择特定的火车名称,我必须显示相应火车的时间。现在我必须将我的数据库时间与当前时间进行比较。如果在当前时间的一小时后,我的数据库时间意味着我必须将其存储到单独的数组中,而在 2 小时后意味着我必须将其存储到单独的数组中。我的代码在这里。

     ArrayList<String> str_arr1 = new ArrayList<String>();
 ArrayList<String> str_arr2 = new ArrayList<String>();
 ArrayList<Integer> hr_arr_1 = new ArrayList<Integer>();
 ArrayList<Integer> mint_arr_1 = new ArrayList<Integer>();
 ArrayList<Integer> hr_arr_2 = new ArrayList<Integer>();
 ArrayList<Integer> mint_arr_2 = new ArrayList<Integer>();


     String str,str1;
 final ArrayList<HashMap<String, String>> new_item = new ArrayList<HashMap<String, String>>(); 
 int[] str_int = new int[3];
 int[] str_int1 = new int[3];
   @SuppressLint("NewApi")
   public class SheduleActivityMain extends Activity
   {
     int count=0;
 public void onCreate(Bundle savedInstanceState) 
 {

    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Calendar now = Calendar.getInstance();
Calendar now1 = Calendar.getInstance();
Calendar now2 = Calendar.getInstance();
   System.out.println("Current time : " + dateFormat.format(now.getTime()));
   String time_now = dateFormat.format(now.getTime());
   System.out.println("time_now----->"+time_now);
   now1.add(Calendar.HOUR,1);

   System.out.println("New time after adding 1 hours : "
                     + dateFormat.format(now1.getTime()));
   now2.add(Calendar.HOUR,2);

   System.out.println("New time after adding 2 hours : "
                     + dateFormat.format(now2.getTime()));
   System.out.println("Is now1 after now ? : " + now1.after(now));

   StringTokenizer st1 = new StringTokenizer(time_now, ":");
   while (st1.hasMoreElements())
    {
        //System.out.println(st1.nextElement());
        str = (String) st1.nextElement();
        str_arr1.add(str);
    } 
        str_int[0] = Integer.parseInt(str_arr1.get(0));
       str_int[1] = Integer.parseInt(str_arr1.get(1));
       str_int[2] = Integer.parseInt(str_arr1.get(2));
     for(int j=0;j<str_int.length;j++)
        {
         System.out.println("integer array is... "+j+"..."+str_int[j]);
        }


       for(int time=0;time<train_time.length;time++)
       {

            StringTokenizer st2 = new StringTokenizer(train_time[time], ":");

            while (st2.hasMoreElements())
            {
                str1 = (String) st2.nextElement();
                str_arr2.add(str1);
                System.out.println("str1..."+str1);

            }   

            System.out.println("str_arr2.........."+str_arr2);
     System.out.println("method calling..........");

       getTrainTime(str_arr2.get(count),str_arr2.get(count+1));

       for(int chk=count;chk<=str_arr2.size()-3;chk++)
       {
           getTrainTime(str_arr2.get(count),str_arr2.get(count+1));
       }

}


    void getTrainTime(String s1,String s2)
    {
        System.out.println("count--->"+count);
        int n1 = Integer.parseInt(s1);
        int n2 = Integer.parseInt(s2);
        System.out.println("n1.n2  "+n1+n2);
        count = count+3;
        System.out.println(str_int[0]+str_int[1]);
        if(n1==str_int[0]+1)
        {
            System.out.println("inside if condition.....");
            hr_arr_1.add(n1);
            //mint_arr_1.add(n2);
            System.out.println("hr_arr_1,mint_arr_1"+hr_arr_1+mint_arr_1);

        }
        else if(n1==str_int[0]+2 )
        {

        }
        //System.out.println("hr_arr_1,mint_arr_1"+hr_arr_1+mint_arr_1);

      }

 }

我遇到了这样的错误..

java.lang.IndexOutOfBoundsException:索引 12 无效,大小为 12

如何比较两次?如何解决此错误?有谁能够帮我?

4

1 回答 1

0
java.lang.IndexOutOfBoundsException: Invalid index 12, size is 12

此错误意味着,您尝试获取第 13 位的值,但您的数组只有 12 个条目。(-> Index ouf of bounds)。请记住:数组索引从 0 开始!

你应该使用 sth likeyourArray[yourArray.length - 1]

编辑: 请下次修改您的代码。它不可读(删除不必要的 System.out.print,给变量一个描述名称......)

顺便说一句,要检查值是否在接下来的两个小时内,您可以使用Calendar

Calendar currentTime = Calendar.getInstance();

Calendar currentTimePlus2 = Calendar.getInstance();
currentTimePlus2.add(Calendar.HOUR,2);

Calendar yourDataTime; //where ever it might come

if( yourDataTime.getTime().before(currentTimePlus2.getTime()) && 
    yourDataTime.getTime().after(currentTime.getTime()){

    //your Data within the next two hours
} else{

   //your Data NOT within the next two hours
}

)

于 2013-10-07T13:45:21.653 回答