标签:过程 参数 第一个 readline java编程思想 lam 个数 put bean
《Java编程思想》读书笔记
1 /** 2 * @author zlz099: 3 * @version 创建时间:2017年9月1日 下午4:03:59 4 */ 5 public class UsingStringBuilder { 6 7 public static Random random = new Random(47); 8 public String toString(){ 9 StringBuilder result = new StringBuilder("["); 10 for(int i = 0; i < 25;i++){ 11 result.append(random.nextInt(100)); 12 result.append(". "); 13 } 14 result.delete(result.length()-2, result.length()); 15 result.append("]"); 16 return result.toString(); 17 } 18 public static void main(String[] args) { 19 // TODO Auto-generated method stub 20 UsingStringBuilder usb = new UsingStringBuilder(); 21 System.out.println(usb); 22 } 23 24 }
1 import java.io.PrintStream; 2 import java.util.*; 3 4 /** 5 * @author zlz099: 6 * @version 创建时间:2017年9月19日 下午3:50:23 7 */ 8 public class Turtle { 9 private String name; 10 private Formatter f; 11 public Turtle(String name,Formatter f){ 12 this.name = name; 13 this.f = f; 14 } 15 public void move(int x,int y){ 16 f.format("%s The Turtle is at(%d,%d)\n",name,x,y); 17 } 18 public static void main(String[] args) { 19 // TODO Auto-generated method stub 20 PrintStream outAlias = System.out; 21 Turtle tommy = new Turtle("Tommy", new Formatter(System.out)); 22 Turtle terry = new Turtle("Terry", new Formatter(outAlias)); 23 tommy.move(0, 0); 24 terry.move(4, 8); 25 tommy.move(3, 4); 26 terry.move(2, 5); 27 tommy.move(3, 3); 28 terry.move(3, 3); 29 30 } 31 32 }
1 import java.util.Formatter; 2 3 /** 4 * @author zlz099: 5 * @version 创建时间:2017年9月19日 下午4:25:05 6 */ 7 public class Recipe { 8 private double total = 0; 9 private Formatter formatter = new Formatter(System.out); 10 public void printTitle(){ 11 formatter.format("%-15s %5s %10s\n", "Item","Qty","Price"); 12 formatter.format("%-15s %5s %10s\n", "----","---","-----"); 13 } 14 public void print(String name,int qty,double price){ 15 formatter.format("%-15s %5s %10.2f\n", name,qty,price); 16 total += price; 17 } 18 public void printTotal(){ 19 formatter.format("%-15s %5s %10.2f\n", "Tax","",total*0.06); 20 formatter.format("%-15s %5s %10s\n", "","","-----"); 21 formatter.format("%-15s %5s %10.2f\n", "Total","",total*1.06); 22 } 23 public static void main(String[] args) { 24 // TODO Auto-generated method stub 25 Recipe recipe = new Recipe(); 26 recipe.printTitle(); 27 recipe.print("Jack‘s Magic Beans", 4, 4.25); 28 recipe.print("Princess peas", 3, 5.1); 29 recipe.print("Three Bears Porridge", 1, 14.29); 30 recipe.printTotal(); 31 } 32 }
十六进制的转储工具:
1 import java.io.File; 2 import java.util.function.BinaryOperator; 3 4 /** 5 * @author zlz099: 6 * @version 创建时间:2017年9月19日 下午4:48:11 7 * 十六进制转储工具(dump) 8 */ 9 public class Hex { 10 public static String format(byte[] data){ 11 StringBuilder result = new StringBuilder(); 12 int n = 0; 13 for(byte b:data){ 14 if(n%16==0){ 15 result.append(String.format("%05X: ", n)); 16 } 17 result.append(String.format("%02X ", b)); 18 n++; 19 if(n%16 == 0) result.append("\n"); 20 } 21 result.append("\n"); 22 return result.toString(); 23 } 24 public static void main(String[] args) throws Exception { 25 // TODO Auto-generated method stub 26 if(args.length == 0){ 27 System.out.println(format(BinaryFile.read("Hex.class"))); 28 } 29 else { 30 System.out.println(format(BinaryFile.read(new File(args[0])))); 31 } 32 } 33 34 }
1 import java.io.BufferedInputStream; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.nio.BufferUnderflowException; 6 7 /** 8 * @author zlz099: 9 * @version 创建时间:2017年9月19日 下午4:56:17 10 */ 11 public class BinaryFile { 12 public static byte[] read(File bFile)throws IOException{ 13 BufferedInputStream bf = new BufferedInputStream(new FileInputStream(bFile)); 14 try { 15 byte[] data = new byte[bf.available()]; 16 bf.read(data); 17 return data; 18 } finally { 19 // TODO: handle exception 20 bf.close(); 21 } 22 } 23 public static byte[] read(String bFile)throws IOException{ 24 return read(new File(bFile).getAbsoluteFile()); 25 } 26 }
1 import java.util.Arrays; 2 3 /** 4 * @author zlz099: 5 * @version 创建时间:2017年9月20日 上午9:53:58 6 */ 7 public class Splitting { 8 public static String knights = "Then, when you have found the shrubbery. you must "+ 9 "cut down the mightiest tree in the forest... "+ 10 "with... a herring!"; 11 public static void split(String regex){ 12 System.out.println(Arrays.toString(knights.split(regex))); 13 } 14 public static void main(String[] args) { 15 // TODO Auto-generated method stub 16 split(" "); 17 split("\\W+"); 18 split("n\\W+"); 19 } 20 21 }
1 /** 2 * @author zlz099: 3 * @version 创建时间:2017年9月20日 上午9:57:46 4 */ 5 public class Replacing { 6 static String s = Splitting.knights; 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 System.out.println(s.replaceFirst("f\\W+", "locaked"));//替换第一个f开头的单词 10 System.out.println(s.replaceAll("shrubbery|tree|herring", "banana"));//替换所有匹配的,|表示或 11 } 12 }
1 import java.util.regex.Matcher; 2 import java.util.regex.Pattern; 3 4 /** 5 * @author zlz099: 6 * @version 创建时间:2017年9月20日 上午11:11:04 7 * 用来在CharSequence中查找多个匹配 8 */ 9 public class Finding { 10 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 Matcher m = Pattern.compile("\\w+").matcher("Evening is full of the linnet‘s wings"); 14 while(m.find()){ 15 System.out.print(m.group()+" "); 16 } 17 System.out.println(); 18 int i = 0; 19 while(m.find(i)){ 20 //接受一个整数作为参数,表示字符串中字符的位置,并以其作为搜索起点。以这个参数作为起点,不断重新设定搜索的起始位置。 21 System.out.print(m.group()+" "); 22 i++; 23 } 24 } 25 26 }
1 import java.util.Arrays; 2 import java.util.regex.Pattern; 3 4 /** 5 * @author zlz099: 6 * @version 创建时间:2017年9月20日 上午10:25:13 7 */ 8 public class SplitDemo { 9 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 String input = "This!!unusual use!!of exclamation!!points"; 13 System.out.println(Arrays.toString(Pattern.compile("!!").split(input))); 14 //第二个split限制将输入分割成字符串的数量 15 System.out.println(Arrays.toString(Pattern.compile("!!").split(input,3))); 16 } 17 }
1 import java.io.BufferedReader; 2 import java.io.StringReader; 3 4 /** 5 * @author zlz099: 6 * @version 创建时间:2017年9月20日 上午10:39:32 7 */ 8 public class SimpleRead { 9 public static BufferedReader input = new BufferedReader(new StringReader("Sir Robin of Camelot\n22 1.61803")); 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 try { 13 System.out.println("What is your name?"); 14 String name = input.readLine(); 15 System.out.println(name); 16 System.out.println("How old are you? What is your favorite double?"); 17 System.out.println("(input: <age> <double>"); 18 String numbers = input.readLine(); 19 System.out.println(numbers); 20 String[] numArray = numbers.split(" "); 21 int age = Integer.parseInt(numArray[0]); 22 double favorite = Double.parseDouble(numArray[1]); 23 System.out.format("Hi %s.\n",name); 24 System.out.format("In 5 years you will be %d.\n",age+5); 25 System.out.format("My favorite double is %f.\n",favorite/2); 26 } catch (Exception e) { 27 // TODO: handle exception 28 System.err.println("I/O exception"); 29 } 30 } 31 32 }
1 import java.util.Scanner; 2 3 /** 4 * @author zlz099: 5 * @version 创建时间:2017年9月20日 上午10:50:07 6 */ 7 public class BetterRead { 8 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 Scanner stdin = new Scanner(SimpleRead.input); 12 System.out.println("What is your name?"); 13 String name = stdin.nextLine(); 14 System.out.println(name); 15 System.out.println("How old are you? What is your favorite double?"); 16 System.out.println("(input: <age> <double>"); 17 int age = stdin.nextInt(); 18 double favorite = stdin.nextDouble(); 19 System.out.println(age); 20 System.out.println(favorite); 21 System.out.format("Hi %s.\n",name); 22 System.out.format("In 5 years you will be %d.\n",age+5); 23 System.out.format("My favorite double is %f.\n",favorite/2); 24 25 } 26 27 }
标签:过程 参数 第一个 readline java编程思想 lam 个数 put bean
原文地址:http://www.cnblogs.com/zlz099/p/7560469.html