簡體   English   中英

Android:數組索引超出范圍錯誤

[英]Android: Array Index Out Of Bounds Error

我試圖將包含日期的字符串轉換為char數組,然后提取月份並將其轉換。

我正在使用API​​,服務器以以下格式提供日期:“ 2015-04-10”,我正嘗試將其轉換為:“ 2015年4月10日”。

  1. 我存儲從服務器收到的日期是一個字符串。
  2. 我將整個字符串轉換為char數組。
  3. 我正在遍歷此數組並將其分為三個數組:YEAR,MONTH,DAY。
  4. 我將這些數組分別轉換為字符串。
  5. 然后,我將'month'字符串與值進行比較,然后使用if-else條件為其指定月份名稱。
  6. 最后,按照我想要的順序添加三個字符串。

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

首先 ,方法不是將數字月份轉換為字符串月份的最佳方法。 使用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];

希望對您有幫助。

您說的地方不會發生此問題。 它可能發生在第一個或第三個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM