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个true
6)生成随机数

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
原创粉丝点击