11 2
,, (Generic) (Collection) : : : :?
(String).. java.lang.stringbuffer. s String s = "abcd"; s = s + "e"; a b c d e a b c d e
,., "910359,, " "910359" " " " " (token) (token),, (delimiter).
java.util.stringtokenizer String s = "910359,, "; StringTokenizer t = new StringTokenizer(s, ","); t.nexttoken(); // "910359" t.nexttoken(); // " " t.nexttoken(); // " " t.nexttoken(); // NoSuchElementException
String s = "$13.46"; StringTokenizer t = new StringTokenizer(s, "$."); t.nexttoken(); // "13" t.nexttoken(); // "46" t.nexttoken(); // NoSuchElementException
class StringTokenizer new StringTokenizer (String text, String delim) text delim nexttoken(): String nexttoken(string new_delimiters): String hasmoretokens(): boolean, 0 nexttoken() counttokens(): int
(Files) (file): (character file): (binary file): 0 1 (open) (close).. (input) : (output) :
FileWriter.. PrintWriter ofile = new PrintWriter (new FileWriter("file.txt")); PrintWriter FileWriter PrintWriter FileWriter print, println print println file
, import java.io.*; IO IOException. public class Output1 { public static void main(string[] args) throws IOException { PrintWriter outfile = new PrintWriter(new FileWriter("test.txt")); outfile.println("hello to you!"); outfile.print("how are"); outfile.println(" you?"); outfile.println(47+2); outfile.close();
FileReader BufferedReader ifile = new BufferedReader (new FileReader("file.txt"));.. BufferedReader BufferedRe ader FileReader FileReader readline readline file
, import java.io.*; import javax.swing.*; public class CopyFile { public static void main(string[] args) throws IOException { String f = JOptionPane.showInputDialog("Input filename, please: "); BufferedReader infile = new BufferedReader(new FileReader(f)); PrintWriter outfile = new PrintWriter(new FileWriter(f + ".out")); while (infile.ready()) { outfile.println(infile.readline()); infile.close(); outfile.close();
GUI
, import java.io.*; import javax.swing.*; public class CopyFile { public static void main(string[] args) throws IOException { JFileChooser chooser = new JFileChooser(); chooser.setdialogtitle("."); int result = chooser.showdialog(null, "Copy"); if(result!= JFileChooser.APPROVE_OPTION) System.exit(0); String f = chooser.getselectedfile().tostring(); BufferedReader infile = new BufferedReader(new FileReader(f)); PrintWriter outfile = new PrintWriter(new FileWriter(f + ".out")); while (infile.ready()) { outfile.println(infile.readline()); infile.close(); outfile.close();
: System.out: System.out.println System.in: System.in InputStream. System.in BufferedReader BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); String s = keyboard.readline();
, 31 20250 42 24500 18 18000! 627750 1029000 324000! PayrollReader.,, PayrollWriter.,.
class PayrollReader Methods getnextrecord(): boolean.. nameof(): String. hoursof(): int. payrateof(): double close()..
import java.io.*; import java.util.*; public class PayrollReader { private BufferedReader infile; private String EOF = "!"; private String name; private int hours, payrate; public PayrollReader(String file_name) { infile = new BufferedReader(new FileReader(file_name)); public String nameof() { return name; public int hoursof() { return hours; public int payrateof() { return payrate; public void close() { infile.close();
public boolean getnextrecord() { if(!infile.ready()) return false; String line = infile.readline(); StringTokenizer t = new StringTokenizer(line, " "); String s = t.nexttoken().trim(); if(s.equals(eof) t.counttokens()!= 2) return false; name = s; hours = new Integer(t.nextToken().trim()).intValue(); payrate = new Integer(t.nextToken().trim()).intValue(); return true;
import javax.swing.*; public class Payroll { private static void processpayroll(string in, String out) { PayrollReader reader = new PayrollReader(in); PayrollWriter writer = new PayrollWriter(out); while(reader.getnextrecord()) { int pay = reader.hoursof() * reader.payrateof(); writer.printcheck(reader.nameof(), pay); reader.close(); writer.close(); public static void main(string[] args) { String in_name = JOptionPane.showInputDialog("Plead type input payroll name: "); String out_name = JOptionPane.showInputDialog("Plead type output payroll name: "); if(in_name!= null && out_name!= null) processpayroll(in_name, out_name);
getnextrecord??.?. false (log)!
? infile.readline() IOException. false. t.counttoken()!= 2
try, catch. catch try { ;... catch ( ) { ;...
public boolean getnextrecord() { if(!infile.ready()) return false; String line = infile.readline(); StringTokenizer t = new StringTokenizer(line, " "); String s = t.nexttoken().trim(); if(s.equals(eof) t.counttokens()!= 2) return false; name = s; hours = new Integer(t.nextToken().trim()).intValue(); payrate = new Integer(t.nextToken().trim()).intValue(); return true;
public boolean getnextrecord() { boolean result = false; try { if(infile.ready()) return false; String line = infile.readline();... if(!s.equals(eof) && t.counttokens() == 2) {... result = true; catch (IOException e) { System.out.println("PayrollReader error: " + e.getmessage()); return result;
public boolean getnextrecord() { boolean result = false; try { if(infile.ready()) return false; String line = infile.readline();... if(!s.equals(eof)) { if(t.counttokens() == 2) {... result = true; else { System.out.println("PayrollReader: bad record format: " + line + " Skipping record"); result = getnextrecord(); catch (IOException e) { System.out.println("PayrollReader error: " + e.getmessage()); return result;
public boolean getnextrecord() { boolean result = false; try {... if(!s.equals(eof)) { if(t.counttokens() == 2) {... result = true; else throw new RuntimeException(line); catch (IOException e) { System.out.println("PayrollReader error: " + e.getmessage()); catch (RuntimeException e) { System.out.println("PayrollReader error: bad record format: " + e.getmessage() + " Skipping record"); result = getnextrecord(); return result;
., Exception in thread "main" java.io.filenotfoundexception: /Volumes/Pamela/ Users/oukseh/11.csv (Permission denied) at java.io.fileinputstream.open(native Method) at java.io.fileinputstream.<init>(fileinputstream.java:120) at java.io.fileinputstream.<init>(fileinputstream.java:79) at java.io.filereader.<init>(filereader.java:41) at CopyFile.main(CopyFile.java:11)
getmessage(): String tostring(): String. printstacktrace (),
Object Throwable Exception InterruptedException RuntimeException IOException ArithmeticException ArrayIndexOutOfBounds Exception ClassCastException NullPointerException FileNotFoundExcepti EOFException InterruptedIOExceptio
catch catch(< > e) {... < >. Exception RuntimeException IOException
, private int readanintfrom(bufferedreader view) throws IOException { int num; try { System.out.print("Type an int: "); String s = view.readline(); num = new Integer(s).intValue(); catch (Exception e) { System.out.println("Error: " + e.getmessage() + " not an integer; try again."); num = readanintfrom(view); // restart return num; Exception RuntimeException RuntimeException NumberFormatException
throws.. Java RuntimeException..
public class ExceptionExample { public ExceptionExample() { public void f() { try { g(); catch (RuntimeException e) { System.out.println("caught at f"); System.out.println("f completes"); public void g() { try { PrintWriter outfile = new PrintWriter(new FileWriter("text.out")); try { outfile.println( h() ); catch (NullPointerException e) { System.out.println("null pointer caught at g"); catch (IOException e) { System.out.println("io error caught at g"); System.out.println("g completes"); private int h() { int[] r = new int[2]; return r[3]; // ArrayIndexOutOfBoundsException! f?
A, B. try {... catch (?) {... 1: throw new RuntimeException("A"); throw new RuntimeException("B"); try {... catch (RuntimeException e) { String m = e.getmessage(); if(m.equals("a"))... if(m.equals("b"))...
2: class MyExceptionA extends Exception { class MyExceptionB extends Exception { throw new MyExceptionA(); throw new MyExceptionB(); try {... catch (MyExceptionA e) {... catch (MyExceptionB e) {...
(Collection)
(Collection) (element),,,
(Collection)
Collection<E> Map<K, V> Set<E> List<E> Queue<E> 인터페이스 클래스 HashSet<E> ArrayList<E> Vector<E> LinkedList<E> HashMap<K, V> Stack<E>
(Generic) : Vector <E> E Vector <Integer>, Vector<String> int, double. Wrapper (Integer, Double)
<E>,,,,
import java.util.vector; public class VectorEx { public static void main(string[] args) { int sum = 0; // Vector<Integer> v = new Vector<Integer>(); v.add(5); v.add(4); v.add(-1);// 5, 4, -1 v.add(2, 100); // 4-1 100 System.out.println(" : " + v.size()); System.out.println(" : " + v.capacity()); // for(int i=0; i<v.size(); i++) { int n = v.get(i); // i System.out.println(n); // int sum = 0; for(int i=0; i<v.size(); i++) { int n = v.elementat(i); // i sum += n; System.out.println(" : " + sum);
import java.util.vector; class Point { private int x, y; public Point(int x,int y) {this.x = x; this.y = y; public String tostring() { return "(" + x + "," + y + ")"; public class PointerVectorEx { public static void main(string[] args) { Vector<Point> v = new Vector<Point>(); // 3 Point v.add(new Point(2, 3)); v.add(new Point(-5, 20)); v.add(new Point(30, -8)); v.remove(1); // 1 Point(-5, 20) // Point for(int i=0; i<v.size(); i++) { Point p = v.get(i); // i Point System.out.println(p); // p.tostring() p
HashMap <K, V> (key) (value) K: V: put(), get() :
import java.util.*; public class HashMapDicEx { public static void main(string[] args) { // HashMap HashMap<String, String> dic = new HashMap<String, String>(); // 3 (key, value) dic dic.put("baby", " "); // "baby" key, " " value dic.put("love", " "); dic.put("apple", " "); // dic (key, value) // Set Set<String> keys = dic.keyset(); // Set Iterator. Iterator<String> it = keys.iterator(); while(it.hasnext()) { String key = it.next(); // String value = dic.get(key); // System.out.print("(" + key + "," + value + ")"); System.out.println();
// Scanner scanner = new Scanner(System.in); for(int i=0; i<3; i++) { System.out.print("?"); String eng = scanner.next(); // ' ' eng ' ' kor String kor = dic.get(eng); if(kor == null) System.out.println(eng + "."); else System.out.println(kor);
(Sequential Search). public static int search(string key, String[] a) { for (int i = 0; i < a.length; i++) if ( a[i].equals(key) == 0 ) return i; return -1;
? 1 1 1000.? 1 2
? 1 1 1000.?! 1 2 1000 500. (10 7 x 10 3 / (2 x 10 7 ) = 500), 500.
(Binary Search),,
public class BinarySearch { public static int search(string key, String[] a) { return search(key, a, 0, a.length); // : a[lo] <= key <= a[hi-1]. public static int search(string key, String[] a, int lo, int hi) { if (hi <= lo) return -1; int mid = lo + (hi - lo) / 2; int cmp = a[mid].compareto(key); if a.compareto(b): a b -1, 0, -1 (cmp > 0) return search(key, a, lo, mid); else if (cmp < 0) return search(key, a, mid+1, hi); else return mid; public static void main(string[] args) { String[] arr = new String[4]; arr[0] = a"; arr[1] = b"; arr[2] = c"; arr[3] = "d"; System.out.println(search("c", arr));
? N : 1 N / 2 N N/2 N/4 1, 1 2 : log2n 1 2! 1 4! 2! 1 8! 4! 2! 1 16! 8! 4! 2! 1 32! 16! 8! 4! 2! 1 64! 32! 16! 8! 4! 2! 1 128! 64! 32! 16! 8! 4! 2! 1 256! 128! 64! 32! 16! 8! 4! 2! 1 512! 256! 128! 64! 32! 16! 8! 4! 2! 1 1024! 512! 256! 128! 64! 32! 16! 8! 4! 2! 1
? 1 1 1000.?! 1 2 1000 0.001. (log2(10 7 ) x 10 3 ) / (2 x 10 7 ) = 0.001)
(Sorting) N :,,,,,...,,,,...
(Insertion Sort)
: https://gmlwjd9405.github.io/2018/05/06/algorithm-insertion-sort.html
public class Insertion { public static void sort(string[] a) { int N = a.length; for (int i = 1; i < N; i++) for (int j = i; j > 0; j--) if (a[j-1].compareto(a[j]) > 0) exch(a, j-1, j); else break; private static void exch(string[] a, int i, int j) { String swap = a[i]; a[i] = a[j]; a[j] = swap; public static void main(string[] args) { String[] arr = new String[4]; arr[0] = d"; arr[1] = "a"; arr[2] = c"; arr[3] = "b"; sort(arr); for (int i = 0; i<arr.length; i++) System.out.println(arr[i]);
? ( : N) : N-1 : i (n-1) + (n-2) +... + 2 + 1 = n(n-1)/2
(Complexity) (order of growth) : :. : (asymptotic complexity) Θ(f(n)) (n: ). f(n) : k 1 f(n) apple apple k 2 f(n) n 2, 1000 x n 2, 3 x n 2 + 1000 x n Θ(n 2 ), k1, k2 n
(Complexity) Θ(n 2 )
(Complexity) Θ(n) Θ(n 2 ) Θ(1) Θ(n) Θ(1) Θ(log 2 n)
10 7 1 3 10 12 1 2 : http://www.cs.princeton.edu/courses/archive/fall09/cos126/
(Merge Sort)
: https://gmlwjd9405.github.io/2018/05/06/algorithm-merge-sort.html
public class Merge { public static void sort(string[] a) {sort(a, 0, a.length); // Sort a[lo, hi). public static void sort(string[] a, int lo, int hi) { int N = hi - lo; if (N <= 1) return; // Recursively sort left and right halves. int mid = lo + N/2; sort(a, lo, mid); sort(a, mid, hi); // Merge sorted halves String[] aux = new String[N]; // Merge into auxiliary array. int i = lo, j = mid; for (int k = 0; k < N; k++) { if (i == mid) aux[k] = a[j++]; else if (j == hi) aux[k] = a[i++]; else if (a[j].compareto(a[i]) < 0) aux[k] = a[j++]; else aux[k] = a[i++]; // Copy back. for (int k = 0; k < N; k++) a[lo + k] = aux[k];
, Θ(nlog 2 n)
(10 ) 10 7 3 3 10 12 2 : http://www.cs.princeton.edu/courses/archive/fall09/cos126/
,, (Generic) (Collection) : : : :?