2012년 1월 11일 수요일

배열을 문자열 또는 리스트로 변환

배열을 문자열로 변환: java.util.Arrays 클래스의 toString() 함수를 이용
String[] array = new String[]{
"a",
"b",
"c"
};
String text = Arrays.toString(array);
System.out.printf("%s\n", text);

결과: [a, b, c]

배열을 리스트로 변환: java.util.Arrays 클래스의 asList() 함수를 이용
String[] array = new String[]{
"a",
"b",
"c"
};
List<String> list = Arrays.asList(array);
System.out.printf("%s\n", list);

결과: [a, b, c]


!) Arrays 클래스를 이용하게 되는 경우 ', ' 구분자에 [] 로 쌓여 있는 문자열을 얻게 된다.
다른 형식의 문자열 결과를 원하는 경우 아래 링크를 참조한다.
Java - How to convert Array to String: http://www.easywayserver.com/blog/java-how-to-convert-array-to-string/

2011년 8월 8일 월요일

List 일부분만 정렬 하기

참고: http://stackoverflow.com/questions/5164902/sorting-a-part-of-java-arraylist
List.subList(int fromIndex, int toIndex) 함수를 사용한다.

예)
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collections;

public class Main {
 public static void main(String[] args) throws Exception {
  ArrayList<String> list = new ArrayList<String>();
  
  list.add("i");
  list.add("e");
  list.add("a");
  list.add("b");
  list.add("g");
  list.add("c");
  list.add("d");
  list.add("f");
  list.add("h");
  
  Collections.sort(list.subList(1, list.size()-1));
  
  System.out.println(list.toString());
  
 }
}
결과:
[i, a, b, c, d, e, f, g, h]

2011년 8월 3일 수요일

file lastModifed 값을 Date 로 변경

File file = new File("myfile"); 
 
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date(file.lastModified()));
cal.getTime(); // Date 값을 return 한다.