我正在做一些火车时刻表应用程序。我已经为他们的相应时间表存储了火车和时间。如果我选择特定的火车名称,我必须显示相应火车的时间。现在我必须将我的数据库时间与当前时间进行比较。如果在当前时间的一小时后,我的数据库时间意味着我必须将其存储到单独的数组中,而在 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
如何比较两次?如何解决此错误?有谁能够帮我?