cheat sheet - 27kim/Programmers GitHub Wiki

  • 배열, Collection 전환
        final int LIMIT = 10;
        String source = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        //배열을 List로 변경
        List list = Arrays.asList(strArr);
        //또는 가변적인 List로 만들고 싶다면
        list = new ArrayList(Arrays.asList(strArr));
        //배열의 정렬
        Arrays.sort(strArr);

        List list = Arrays.asList(strArr);

        List <String> list2 = list.subList(0,10);
        
        String [] returnArr = list2.toArray(new String [list2.size()]);

  • Map에서 Iterator 사용하기
Map map = new HashMap();
        
Iterator it = map.keySet().iterator();
  • Map에서 keySet 사용하기
			FileWriter fw = new FileWriter("./REPORT_2.TXT");
			Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();

			while (iterator.hasNext()) {
				Entry<String, Integer> data = iterator.next();
				String str = data.getKey() + "#" + data.getValue() + "\n";
				fw.write(str);
			}

			fw.close();

  • 외부 프로그램 사용
public class RunManager {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {

			BufferedReader br = new BufferedReader(new FileReader("./LOGFILE_B.TXT"));

			Map<String, Integer> map = new HashMap();
			String s;
			String line;
			List<String> list = new ArrayList();
			while ((line = br.readLine()) != null) {

				String[] input = line.split("#");
				String key = input[1];
				String conValue = "";

				Process oProcess = new ProcessBuilder("CODECONV.EXE", input[2]).start();
				BufferedReader stdOut = new BufferedReader(new InputStreamReader(oProcess.getInputStream()));
				while ((s = stdOut.readLine()) != null)
					conValue = s;

				if (map.containsKey(key)) {
					map.put(key, map.get(key) + 1);
				} else {
					map.put(key, 0);
				}
				list.add(input[0] + "#" + input[1] + "#" + conValue);

			}
			br.close();

			for (String dataInput : list) {
				String[] input = dataInput.split("#");

				FileWriter fw = new FileWriter("./REPORT_3_" + input[1] + ".TXT", true);
				Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();

				fw.write(input[0] + "#" + input[1] + "#" + input[2] + "\n");

				fw.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
⚠️ **GitHub.com Fallback** ⚠️