[英]Android: Array Index Out Of Bounds Error
我试图将包含日期的字符串转换为char数组,然后提取月份并将其转换。
我正在使用API,服务器以以下格式提供日期:“ 2015-04-10”,我正尝试将其转换为:“ 2015年4月10日”。
Android Studio说: java.lang.ArrayIndexOutOfBoundsException: length=2; index=5
java.lang.ArrayIndexOutOfBoundsException: length=2; index=5
这是我的代码:(“ temp”是包含日期的字符串)
//TODO: Here lies the method which will apparently convert the movie string to human read-able format...... Use it with caution.
char release[] = temp.toCharArray();
Log.d("LOG", release.toString());
char date[] = new char[2];
char year[] = new char[4];
char month[] = new char[2];
for (int i = 0; i < 11; i++) {
try {
if (i > 4 && i < 7) {
month[i] = release[i];
}
if (i < 4) {
year[i] = release[i]; // ---> Android Studio says error on this line...
}
if (i > 7 && i < 10) {
date[i] = release[i];
}
} catch (Error error) {
error.printStackTrace();
}
}
String monthString = month.toString();
String dateString = date.toString();
String yearString = year.toString();
String finalReleaseDate;
if (monthString.contentEquals("01")) {
monthString = "January";
} else if (monthString.contentEquals("02")) {
monthString = "February";
} else if (monthString.contentEquals("03")) {
monthString = "March";
} else if (monthString.contentEquals("04")) {
monthString = "April";
} else if (monthString.contentEquals("05")) {
monthString = "May";
} else if (monthString.contentEquals("06")) {
monthString = "June";
} else if (monthString.contentEquals("07")) {
monthString = "July";
} else if (monthString.contentEquals("08")) {
monthString = "August";
} else if (monthString.contentEquals("09")) {
monthString = "September";
} else if (monthString.contentEquals("10")) {
monthString = "October";
} else if (monthString.contentEquals("11")) {
monthString = "November";
} else if (monthString.contentEquals("12")) {
monthString = "December";
}
finalReleaseDate = dateString + " " + monthString + ", " + yearString;
movieModel.setReleaseDate(finalReleaseDate);
logcat中的消息是: java.lang.ArrayIndexOutOfBoundsException: length=2; index=5
java.lang.ArrayIndexOutOfBoundsException: length=2; index=5
(我已在显示此错误的行上进行了标记。)
请帮忙!!
您要在0到11的i上进行迭代,所以发布数组仅包含10位数字,因此您应该在0到9之间进行迭代
哦,这不是您的问题,您的主要问题是那一年只包含4个对象(从0到3),所以当我大于3时,您将无法获得项目i
那是我猜不到的吗...日志猫说index=5
所以当我为5时发生了错误,但是当我小于4时就包括了您要说的行,所以我想您看错了日志猫。
而且它还说长度为2,所以在读取日期数组或月份数组时会崩溃,但在读取年份数组时却没办法
我在评论中以@Ken Wolf Wolf的身份简要介绍了您可以使用SimpleDateFormat
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-mm-dd", Locale.getDefault());
Date date = simpleDate.parse("");
simpleDate = new SimpleDateFormat("dd MMMM, yyyy.", Locale.getDefault());
String newFormatString = simpleDate.format(date);
首先 ,方法不是将数字月份转换为字符串月份的最佳方法。 使用Date
和SimpleDateFormat
方法进行转换应该更容易,更稳定。
其次 ,您为崩溃标记了错误的行。 崩溃(java.lang.ArrayIndexOutOfBoundsException: ***length=2; index=5***)
告诉您index为5且数组长度为4 。
if (i < 4) {
year[i] = release[i]; //index 5 cannot be called here and year lenght is 4
}
if (i > 4 && i < 7) {
month[i] = release[i]; //index 5 is called here and month lenght is 2
}
if (i > 7 && i < 10) {
date[i] = release[i];
}
发生崩溃是因为您正在调用month[5] = release[5];
而月份长度仅为2。
为了避免这种情况,请将您的代码更改为:
if (i<4) {
//2015 index 0,1,2,3
year[i] = release[i];
} else if (i>4 && i<7) {
//04 index 5,6
month[i-5] = release[i];
} else if (i>7 && i<10) {
//10 index 8,9
date[i-8] = release[i];
}
第三 ,如果您希望以这种方式获取年,月,日,则这种方式会更容易。
String [] date_splited = server_string.split("-");
String year = date_splited[0];
String month = date_splited[1];
String day = date_splited[2];
希望对您有帮助。
您说的地方不会发生此问题。 它可能发生在第一个或第三个if语句中。 因为date []和month []的长度都只有2,所以您尝试访问索引大于2的那个。
正确的方法应该使用Omar标记的SimpleDateFormat。 一旦有了日期(或日历,可能取决于信息的用途),就可以使用本地设置来翻译它。
为什么使用char []? 您可以直接使用String []。 在字符串中,有可用的拆分方法。 因此,使用拆分方法可以将日期月份和年份分开。 您可以使用以下代码:
String date= "2015-04-10";
String[] temp= date.split("-");
temp [0]为2015,temp [1]为04,temp [1]为10。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.