Java编程思想:数据存储
来源:互联网 发布:杨颖人品知乎 编辑:程序博客网 时间:2024/06/06 20:04
1)String : "abc" new String("abc")。此时建立了两个字符串对象。每当用String操作字符串时,实际上是在不断的创建新的对象,而原来的对象就会变为垃圾被GC回收掉;
String str = “This is only a” + “ simple” + “ test”;StringBuffer builder = new StringBuilder(“This is only a”).append(“ simple”).append(“ test”);
你会很惊讶的发现,生成str对象的速度简直太快了,而这个时候StringBuffer居然速度上根本一点都不占优势。其实这是JVM的一个把戏,实际上:
String str = “This is only a” + “ simple” + “test”;
其实就是:
String str = “This is only a simple test”
所以不需要太多的时间了。但大家这里要注意的是,如果你的字符串是来自另外的String对象的话,速度就没那么快了,譬如:
String str2 = “This is only a”;
String str3 = “ simple”
String str4 = “ test”;
String str1 = str2 +str3 + str4;
这时候JVM会规规矩矩的按照原来的方式去做。
StringBuilder:线程非安全的 StringBuffer:线程安全的
String s;s+="abc"//String 本质为只读类型
2)格式化输出
String.fromat()
3)IO操作:
public static void main(String [] args) throws IOException{ System.out.print(“Enter a Char:”); char i = (char) System.in.read(); System.out.println(“your char is :”+i); } }
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; System.out.println(“Enter your value:”); str = br.readLine();
Scanner sc = new Scanner(System.in); System.out.println(“请输入你的姓名:”); String name = sc.nextLine(); System.out.println(“请输入你的年龄:”); int age = sc.nextInt();
4)StringTokenizer
String input=" ";StringTokenizer stoke=new StringTokenizer(input);while(stoke.hasMoreElement){stoke.nextToekn}5)Arrays.fill():编写测试用例常用
int []a=new int[10];Arrays.fill(a,true)//存储了10个true6)生成随机数
Random r=new Random(47);r.nextBoolean();//r.nextInt();
7)java.util包中提供了两种顶层接口:1集合类Collection(queue,List元素有序允许重复,Set集合无序不允许重复);2映射类Map。对于集合类,必须在初始化时指定存储对象的类型。
8)List:ArrayList和LinkedList都不是线程安全
List<Type> test=new ArrayList<Type>()9)for循环:
Collection<Integer> c=new ArrayList<Integer>();for(Interger i:c){}
10)Arrays.asList():数组转换成List类型。Collections.addAll():将一个List全部追加到另一个list
Collection<Integer> c=new ArrayList<Integer>(Arrays.asList());Collections.addAll(collection,array);
11)the behaviors of Queues and stacks is provided via LinkedList
12)HashMap is for rapid access whereas TreeMap keeps its keys in order;LinkedHashMap keeps its elements in insertation order.
13)HashSet的性能(特别是常用的插入和插入)总比TreeSet好,TreeSet可以保持元素的排序状态。
14) String ->int
i=Integer.parseInt(s);i=Integer.valueOf(s).intValue();
int ->String
s=String.valueOf(i);15)
br = new BufferedReader(new FileReader(path)); String line = null; StudentInfo student = null; while ((line = br.readLine()) != null) { }
bw = new BufferedWriter(new FileWriter(dstPath)); if (studentList != null && !studentList.isEmpty()) { for(StudentInfo stu:studentList) { bw.write(stu.getId()+","+stu.getName()+","+stu.getBirthday()+","+stu.getScore()); bw.newLine();//换行 } } bw.flush();//强制输出缓冲区的内容,避免数据缓存,造成文件写入不完整的情况。 } catch (IOException e) { throw new Exception("目标文件未找到", e); } finally{
bw.close(); }
16)文件字节流
temp中生成一个名字为dt.txt文件建FileInputStream对象
FileInputStream in=new FileInputStream("d: /abc.txt");
创建FileOutputStream对象
FileOutputStream out=new FileOutputStream("d: /abc.txt");
code1
class Copy{ public static void main(String args[]){ try{ File inFile=new File(“file1.java"); File outFile=new File(“file2.java"); FileInputStream finS=new FileInputStream(inFile); FileOutputStream foutS=new FileOutputStream(outFile); int c; while( (c=finS.read())!=-1 ) { foutS.write(c); } finS.close(); foutS.close(); }catch(IOException e) {System.err.println("FileStreamsTest: "+e);}}}code2
新建一个文件夹为temp
File filePath=new File("temp");if(!filePath.exists()){ filePath.mkdir();}
在temp中生成一个名字为dt.txt文件;创建FileOutputStream对象,使之与dt.txt关联
File f1=new File(filePath,"dt.txt");FileOutputStream fout =new FileOutputStream(f1);
0 0
- Java编程思想:数据存储
- 《java编程思想》之java对象的存储
- 《JAVA编程思想》学习笔记-JAVA存储位置说明
- java 编程思想 末端哨兵实现内部链式存储机制
- java oop思想编程思想
- JAVA编程思想(第4版) 静态数据的初始化
- 69.JAVA编程思想——数据报
- 《Java编程思想》学习之对象入门1.6数据保存
- 关于编程思想和《java编程思想》
- Java编程思想读书笔记
- Java编程思想:引言
- java编程思想:
- Java编程思想学习
- Java编程思想 笔记
- java编程思想读书笔记
- java编程思想
- java 编程思想 读书笔记
- 学习java编程思想
- Android 自定义View 仿蚂蚁信用分析(正多边形)
- Linux中find常见用法示例
- 放开封闭原则(OCP)——例子一
- php断点续传之乱序合并文件
- redis.conf 配置项说明
- Java编程思想:数据存储
- 字符编码查看工具
- Java经典书籍推荐
- Unable to lock the administration directory (/var/lib/dpkg/), is another process
- Android Edittext不可编辑
- 解决相对路径问题。
- 免费馅饼
- CSS inliner换行 和 animation content的使用小记
- 二、eclipse中项目编译不生成.class文件