【问题标题】:Android: Array Index Out Of Bounds ErrorAndroid:数组索引越界错误
【发布时间】:2017-07-26 16:30:59
【问题描述】:

我正在尝试将包含日期的字符串转换为 char 数组,然后提取月份并进行转换。

我正在使用 API,服务器提供的日期格式为:“2015-04-10”,我正在尝试将其转换为:“2015 年 4 月 10 日”。

  1. 我将从服务器接收到的日期存储在一个字符串中。
  2. 我正在将整个字符串转换为一个字符数组。
  3. 我在这个数组中运行一个循环并将它分成三个数组: 年月日。
  4. 我将这些数组分别转换为字符串。
  5. 然后我将“月”字符串与值进行比较,然后使用 if-else 条件为其分配月份名称。
  6. 最后,我将按照我想要的顺序添加三个字符串。

Android Studio 说: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(我已经标记了显示此错误的行..)

请帮忙!!

【问题讨论】:

  • 尝试使用SimpleDateFormat,它会为你省去很多痛苦:docs.oracle.com/javase/7/docs/api/java/text/…
  • 一个明显的错误是,比如说i=5 所以month[i] 异常,做一些调试并按照上面的建议做
  • 你是一个声明一个大小为 2 的数组: char month[] = new char[2];并制作一个比这更大的循环 for (int i = 0; i 4 && i

标签: java android arrays string android-studio


【解决方案1】:

首先,您的方法不是将数字月份转换为字符串月份的最佳方法。使用DateSimpleDateFormat 方法转换应该更容易更稳定。


第二,你为崩溃标记了错误的行。 此崩溃 (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];

希望对你有所帮助。

【讨论】:

  • P.S.第4段应该是String year = date_splited[0]....谢谢解释!!
【解决方案2】:

你为什么使用 char[] ?您可以直接使用 String[] 。在字符串中有可用的拆分方法。因此,使用拆分方法可以分隔日期月份和年份。您可以使用以下代码:

String date=  "2015-04-10";
String[] temp= date.split("-");

temp[0] 是 2015,temp[1] 是 04,temp[1] 是 10。

【讨论】:

    【解决方案3】:

    你正在迭代 i 从 0 到 11 release 数组仅包含 10 个数字,因此您应该从 0 迭代到 9

    哦,等等,这不是你的问题,你的主要问题是 year 只包含从 0 到 3 的 4 个对象,所以当 ii /em> 大于 3

    我猜不是这样吗... 日志猫说index=5 所以当 i 为 5 时发生错误,但是当 i 小于 4 时包含您所说的行,所以我猜你日志猫读错了。

    而且它还说长度为 2,所以它在读取 date 数组或 month 数组时崩溃,但在读取 year 时没有办法> 数组

    我在你可以使用SimpleDateFormatSimpleDateFormat的评论中简写为@Ken Wolf Wolf

    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);
    

    【讨论】:

      【解决方案4】:

      正确的方法应该是使用 Omar 标记的 SimpleDateFormat。 一旦有了日期(或日历,可能取决于您对信息的用途),您可以使用本地设置来翻译它。

      【讨论】:

        【解决方案5】:

        问题不是发生在您所说的地方。它可能出现在第一个或第三个 if 语句中。因为 date[] 和 month[] 的长度都只有 2,而您正试图以高于 2 的索引访问它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-06-06
          • 2015-03-22
          • 1970-01-01
          • 1970-01-01
          • 2013-07-27
          相关资源
          最近更新 更多