zoukankan      html  css  js  c++  java
  • 从DELPHI到JAVA[delphi]

    1 字符串处理

           DELPHI

                  var s :string;

                  s := s + 'a string';

           JAVA             习惯用法

                  String s = new String();

                  s += 'a string';

                  由于String对象不可修改,导致对String对象赋值会有一个StringBuffer对象产生并销毁,一个新的String对象被产生。在大字符串处理和循环处理时,性能极差。(参见《JAVA 字符串处理速度测试》)

           JAVA             优化用法

                  StringBuffer s = new StringBuffer();       //如果字符串较大,最好能够赋予一个初始的StringBuffer大小。

                  s.Append('a string');

    2 数组

           DELPHI

                  var a:array of string;

                  SetLength(10);

           JAVA             1: Arrays

                  String[] a = new String[10];

                  int [] a = {1,2,3,4}

                  可以直接赋初始值(不管是基本类型还是对象)

                  注意:不可改变数组大小!

                  拥有以下实用函数:full,sort,equals,binarySearch,System.arraycopy

           JAVA             2: ArrayList (LinkedList)

                  ArrayList a = new ArrayList();

                  a.add(new Integer(1));

                  a.get(1);

                  注意:数组的值必须是对象类型!

                  不能用“[]”取值。

                  容器的元素是Object类。(也即使用时需要强制转型)【JDK1.5支持泛型JAVA】

                  ArrayList随机访问性能好,但元素插入和删除性能差。LinkedList反之。

                  LinkedListList中央插入和删除元素性能好,可以用来制作stack,queue或deque。包含下列函数:addFirst,addLast,getFirst,getLast,removeFirst,removeLast

                  要访问一个容器,最好是使用迭代器,例如a.iterator()。

                  迭代器使用:

                  void print(Iterator e) { while (e.hasNext) System.out.println(e.next());}

                  迭代器包含下列函数:next,hasNext,remove

                  注意:当取得一个迭代器后,如果容器被改变,会抛出异常, delphi override 和 overload 的区别

    3 集合

           DELPHI

                  var option : set of char;

                  option := ['a','b'];

           JAVA             1 BitSet

                  大小不限制。

                  性能相比array略差。

           JAVA             2 HashSet (TreeSet)

                  元素必须唯一。

                  每个元素都必须定义equals函数和compareTo函数,delphi pchar string互转函数

                  HashSet查找性能好,但无法排序。必须定义hashCode函数。

                  TreeSet是有序的,但查找性能较差。

                  TreeSet可以产生大小在一定范围内的一批元素。

                  Collection函数说明(包含Set和Map):

    函数名称

    函数说明

    备注

    add

    将对象增加到容器

    可能未被实现

    addAll

    将一批对象增加到容器

    可能未被实现

    clear

    清除所有元素

    可能未被实现

    contains

    检查是否存在元素

     <o:p></o:p>

    containsAll

    检查是否存在一批元素

     <o:p></o:p>

    isEmpty

    容器是否为空

     <o:p></o:p>

    iterator

    返回一个迭代器

     <o:p></o:p>

    remove

    删除一个元素

    可能未被实现

    removeAll

    删除一批元素

    可能未被实现

    retainAll

    删除除了一批元素外的所有元素

    可能未被实现

    size

    容器中元素个数

     <o:p></o:p>

    toArray

    返回一个含所有元素的array

     <o:p></o:p>

    max,min

    取得最大、最小值

     <o:p></o:p>

    copy

    从一个容器复制到另一个容器

     <o:p></o:p>

     <o:p></o:p>

     <o:p></o:p>

     <o:p></o:p>

     <o:p></o:p>

     <o:p></o:p>

     <o:p></o:p>

                 

    4 Key/Vaue对

           DELPHI

                  var sl :TStringList;

                  sl.Values['key'] := 'value';

           JAVA             HashMap (TreeMap,WeakHaspMap)

                  TreeMap可排序,但性能较差。可以返回在一定大小范围内元素的子集。

                  HashMap性能好,不可排序。需要提供hashCode、equals等函数。

                  WeakHaspMap似乎可以节省内存空间。

    5 多线程同步

           JAVA

                  Collection c = Coolections. synchronizedCollection(new ArrayList());

                  List l = Coolections. synchronizedList(new ArrayList());

                  Set s = Coolections. synchronizedSet(new HashSet());

                  Map c = Coolections. synchronizedMap(new HashMap());

    6 文件操作

           DELPHI

                  var fin :TFileStream;

           JAVA             1 InputStream / OutputStream

          

           JAVA             2 RandomAccessFile

          

    7 目录操作

           DELPHI

                  FindFirst,etc.

           JAVA             File

                  String[] list = path.list( new FilenameFilter() {

                         public boolean accept(File dir, String s) {

                                String f = new File(s).getName();

                                return f.indexOf(filter)!=-1;

                         }       //这儿是一个内嵌类(inner class)

                  });

                  File类提供下列函数:getAbsolutePath, getName, getPath, length, lastModified, renameTo, mkdirs, isFile, isDirectory, exists,delete等。

     <o:p></o:p>

    8 压缩

           DELPHI

                  zlib.pas

           JAVA             ZipInputStream / ZipOutputStream

     <o:p></o:p>

    9 RTTI

           DELPHI

                  is, as ,className,typInfo.pas

           JAVA

                  instanceof或isInstance()相当于DELPHI中的is

                  Class.forName("AClass") 相当于 AClass.class

                  对于外覆类例如Integer,其Integer.TYPE 等同于 integer.class

                  Class.forName("AClass").newInstance()可以产生一个新对象(该类必须具有缺省构造函数)。

                  java.lang.reflect提供Field, Method. Constructor等类。

                 

    10 跳出嵌套循环

           DELPHI

                  goto

           JAVA

                  label1:

                  for (int i=0; i<10; i++)

                  for (int j=0; j<10; j++)

                  {

                         continue label1;    //跳转到label1,循环继续执行

                         break label1;           //跳转到label1,循环中止执行

                  }<o:p></o:p>

     <o:p></o:p>

     <o:p></o:p>

    测试:

           建议在编写程序时同时编写测试代码。

     <o:p></o:p>

    其他常用函数:

    函数功能

    DELPHI

    JAVA

    输出调试信息

    writeln

    System.out.println()

    随机函数

    random

    Math.random()

    字符串转换为整数

     

    原文来自: 【100脚本网 www.pc100.net】

  • 相关阅读:
    Insert into select语句把生产服务器炸了!
    人人都能看懂的 6 种限流实现方案
    Idea 快捷生成类注释与方法注释
    拦截器
    java 泛型
    SQL语句总结
    深入浅出Git教程(转载)
    (转载)CSS3与页面布局学习总结(三)——BFC、定位、浮动、7种垂直居中方法
    css中常见margin塌陷问题之解决办法
    css中固定宽高div与不固定宽高div垂直居中的处理办法
  • 原文地址:https://www.cnblogs.com/python001/p/4340771.html
Copyright © 2011-2022 走看看