2011-07-05 38 views
0

我有一個arraylist arr,其中包含一系列數字,如07,52,25,10,19,55,15,18,41。在此列表中,第一項是小時,第二項是分鐘第三個是第二個,就像07:52:25。 現在我想創建一個時間數組,我可以插入這些值並進行一些算術運算,如第一個索引和第二個索引之間的差異,這會給我帶來時間差異。那我該怎麼做?如何創建時間陣列

ArrayList arr = new ArrayList(); 
StringTokenizer st = new StringTokenizer(line, ":Mode set - Out of Service In Service"); 
while(st.hasMoreTokens()){ 
    arr.add(st.nextToken()); 
} 
+1

你確定你不想把它解析成日期或日曆對象,然後對它進行操作嗎?這可以讓你在沒有任何額外麻煩的情況下進行任何時間計算。 – bezmax

+0

爲什麼你不在數組中存儲'Date'或'Calendar'的值? – flash

+4

這是你發佈的第四個問題,附帶相同的代碼... – Giann

回答

1

OK播放我想我明白你想要你的代碼做什麼。如何我會做到這一點。

public class DateHandler 
{ 
    public DateHandler(int seconds, int minutes, int hours) 
    { 
     this.seconds = seconds; 
     this.minutes = minutes; 
     this.hours = hours; 
    } 

    public String toString() 
    { 
     return "Seconds: "+seconds+" Minutes: "+minutes+" Hours: "+hours; 
    } 

    public int seconds; 
    public int minutes; 
    public int hours; 
} 

public class Main 
{ 
    public static void main(String args[]) 
    { 
     int[] data = {07, 52, 25, 10, 19, 55, 15, 18, 41} 
     int numberOfDates = data.length/3//Divide by 3 because there are 3 numbers per date 
     ArrayList<DateHandler> dates = new ArrayList<DateHandler>(numberOfDates); 
     for(int x=0;x<numberOfDates;x++) 
     { 
      int index = x*3; 
      DateHandler date = new DateHandler(data[index],data[index+1],data[index+2]); 
      System.out.println("added date: "+date.toString()); 
      dates.add(date); 
     } 

     //here you can do your calculations. 
    } 
} 

我希望這有助於!

0

我會使用拆分到tokenise。你可以閱讀從多行數據的次數任意數量

BufferedReader br = 
String line; 
List<Integer> times = new ArrayList<Integer>(); 
while((line = br.readLine()) != null) { 
    String[] timesArr = line.split(", ?"); 

    for(int i=0;i<timesArr.length-2;i+=3) 
     times.add(Integer.parseInt(times[i]) * 3600 + 
       Integer.parseInt(times[i+1]) * 60 + 
       Integer.parseInt(times[i+2])); 
} 
br.close(); 

System.out.println(times); // prints three times in seconds. 
// difference between times 
for(int i=0;i<times.size()-1;i++) 
    System.out.println("Between "+i+" and "+(i+1)+ 
     " was "+(times.get(i+1)-times.get(i))+" seconds."); 
+0

謝謝彼得。但是這個數據(時間)只是一個例子。我有很多數據(我的意思是可變長度數組)。我想分幾小時,分鐘和秒。 – Ricky

+0

Split可以在任何長度的字符串上工作。如果您有多行數據,您可以在循環中讀取這些數據,並將它們添加到'times'中。 –

+0

好的。謝謝彼得。 – Ricky

0

如果這個陣列(你說 「的ArrayList ARR」 吧?)

07, 52, 25, 10, 19, 55, 15, 18, 41 

意味着

7:52:25, 10:19:55 and 15:18:41 

然後你可以使用SimpleDateformat將字符串「轉換」爲日期。

SimpleDateFormat可以parse( 「轉換」)一StringDate,並format一個DateString(實際上是一個StringBuffer)。

在你的情況,請通過ArrayList組你循環的小時,分​​鍾和秒,並用SimpleDateFormat解析它們:

int index = 0; 
    String tempTime = ""; 
    ArrayList<Date> dateList = new ArrayList<Date>(); 

    //assuming hour in format 0-23, if 1-24 use k in place of h 
      SimpleDateFormat dateFormat = new SimpleDateFormat("h-m-s-"); 

    for(String timeeElement : timeArrayList) 
    { 
     tempTime += timeeElement; 
     tempTime += "-"; //To handle situations when there is only one digit. 
     index++; 
     if(index % 3 == 0) 
     { 
      Date d = dateFormat.parse(tempTime, new ParsePosition(0)); 
      dateList.add(d); 
      tempTime = ""; 
     } 

    } 

你有日期的列表與現在

+0

感謝您的幫助nivas。 – Ricky