2011-07-05 10 views
0

나는 07, 52, 25, 10, 19, 55, 15, 18, 41과 같은 일련의 숫자를 포함하는 arraylist arr을 가지고 있습니다.이 목록의 첫 번째 항목은 시간, 초는 분입니다. 셋째는 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

Date 또는 Calendar 개체로 파싱하고 작업하고 싶지 않습니까? 그러면 별도의 번거 로움없이 시간을 계산할 수 있습니다. – bezmax

+0

배열에'Date' 또는'Calendar' 값을 저장하지 않는 이유는 무엇입니까? – flash

+4

이것은 동일한 코드를 첨부하여 게시하는 네 번째 질문입니다. – Giann

답변

1

확인 놀 날짜 목록이 나는 당신이 당신의 코드가 원하는 것을 이해한다고 생각한다. 내가 어떻게 할 것인가.

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

나는 split to tokenise를 사용합니다. 이 배열은 (당신이 말한 바로 "편곡 ArrayList에"?)

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

분할은 모든 길이의 문자열에서 작동합니다. 여러 줄의 데이터가있는 경우 루프에서이를 읽고 'times'에 추가 할 수 있습니다. –

+0

좋습니다. 고마워 피터. – Ricky

0

경우 멀티 라인 데이터에서 시간의 양을 읽을 수

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

은 당신이 할 수있는 그런

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

을 의미한다 SimpleDateformat을 사용하여 문자열을 날짜로 "변환"하십시오.

SimpleDateFormatparseDateString를 ("변환") 및 formatDate (실제로는 StringBuffer)는 String합니다.

귀하의 경우에는

, 당신의 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

도와 주셔서 감사합니다. – Ricky

관련 문제