<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Vincent.Chan‘s Blog

    常用鏈接

    統(tǒng)計(jì)

    積分與排名

    網(wǎng)站

    最新評(píng)論

    《Java 5.0 Tiger》Chapter 1

    Chapter 1. What's New?

    1.1 Working with Arrays

    String[] s1 = "1""2""3" };
    System.out.println(Arrays.toString(s1));
    String[][] s2 
    = "1""2""3" }"4""5""6" } };
    System.out.println(Arrays.deepToString(s2));
    String[][] s3 
    = "1""2""3" }"4""5""6" } };
    System.out.println(Arrays.deepEquals(s2, s3));
    /* --- console output --- */
    [123]
    [[
    123], [456]]
    true

    The first method to take note of, at least for Tiger fans, is toString( ). This handles the rather annoying task of printing arrays for you. While this is trivial to write on your own, it's still nice that Sun takes care of it for you now.

    Another similar, but also new, method is deepToString( ). This method takes in an object array, and prints out its contents, including the contents of any arrays that it might contain.

    Finally, Arrays provides a deepEquals( ) method that compares multidimensional arrays.

    1.2 Using Queues

    Another cool collection addition is the java.util.Queue class, for all those occasions when you need FIFO (first-in, first-out) action.

    Queue q = new LinkedList();
    q.offer(
    "First");
    q.offer(
    "Second");
    q.offer(
    "Third");
     Object o;
     
    while ((o = q.poll()) != null)
             System.out.println(o);

    Use offer(), poll() instead of add(), remove() respectivelly. If you want the head without removing it, ues element() or peek().

    In Tiger, LinkedList has been retrofitted to implement the Queue interface. While you can use it like any other List implementation, it can also be used as a Queue implementation.

    1.3 Ordering Queues Using Comparators

    PriorityQueue, a Queue with Comparator. If you don't specify a Comparator, natural ordering occurs.

    import java.util.Comparator;
    import java.util.PriorityQueue;
    import java.util.Queue;

    public class PriorityQueueTester {
        
    public static void main(String[] args) {
            Queue
    <Integer> pq = new PriorityQueue<Integer>(20,
                    
    new Comparator<Integer>() {
                        
    public int compare(Integer i, Integer j) {
                            
    int result = i % 2 - j % 2;
                            
    if (result == 0)
                                result 
    = i - j;
                            
    return result;
                        }

                    }
    );
            
    // Fill up with data, in an odd order
            for (int i = 0; i < 20; i++{
                pq.offer(
    20 - i);
            }

            
    // Print out and check ordering
            for (int i = 0; i < 20; i++{
                System.out.print(pq.poll() 
    + " ");
            }

        }

    }

    /* --- console output --- */

    2 4 6 8 10 12 14 16 18 20 1 3 5 7 9 11 13 15 17 19 

    1.4 Overriding Return Types 

    class Point2D {
        
    protected int x, y;

        
    public Point2D() {
            
    this.x = 0;
            
    this.y = 0;
        }


        
    public Point2D(int x, int y) {
            
    this.x = x;
            
    this.y = y;
        }

    }


    class Point3D extends Point2D {
        
    protected int z;

        
    public Point3D(int x, int y) {
            
    this(x, y, 0);
        }


        
    public Point3D(int x, int y, int z) {
            
    this.x = x;
            
    this.y = y;
            
    this.z = z;
        }

    }


    class Position2D {
        Point2D location;

        
    public Position2D() {
            
    this.location = new Point2D();
        }


        
    public Position2D(int x, int y) {
            
    this.location = new Point2D(x, y);
        }


        
    public Point2D getLocation() {
            
    return location;
        }


    }


    class Position3D extends Position2D {
        Point3D location;

        
    public Position3D(int x, int y, int z) {
            
    this.location = new Point3D(x, y, z);
        }


        
    public Point3D getLocation() {
            
    return location;
        }

    }

    The key is the line public Point3D getLocation( ), which probably looks pretty odd to you, but get used to it. This is called a covariant return, and is only allowed if the return type of the subclass is an extension of the return type of the superclass. 

    1.5 Taking Advantage of Better Unicode

    In Tiger, Java has moved to support Unicode 4.0, which defines several characters that don't fit into 16 bits. This means that they won't fit into a char, and that has some far-reaching consequences. You'll have to use int to represent these characters, and as a result methods like Character.isUpperCase( ) and Character.isWhitespace( ) now have variants that accept int arguments. So if you're needing values in Unicode 3.0 that are not available in Unicode 3.0, you'll need to use these new methods..

    Most of the new characters in Unicode 4.0 are Han ideographs.

    1.6 Adding StringBuilder to the Mix 

    Replace all your StringBuffer code with StringBuilder code. Really—it's as simple as that. If you're working in a single-thread environment, or in a piece of code where you aren't worried about multiple threads accessing the code, or synchronization, it's best to use StringBuilder instead of StringBuffer.

    posted on 2006-02-18 18:28 Vincent.Chen 閱讀(156) 評(píng)論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 亚洲色图在线播放| 亚洲成?v人片天堂网无码| 亚洲国产精品乱码一区二区| 免费精品国产自产拍在线观看| 国产网站在线免费观看| 亚洲国产成人综合精品| 麻豆成人精品国产免费| www.xxxx.com日本免费| 国产乱辈通伦影片在线播放亚洲| 污视频网站免费观看| 亚洲视频在线一区二区| 中文字幕在线免费看| 亚洲图片一区二区| 免费观看激色视频网站bd | 亚洲av无码一区二区三区天堂| 四虎成人免费观看在线网址 | 亚洲喷奶水中文字幕电影| 在线视频观看免费视频18| 亚洲色无码国产精品网站可下载| 免费无码黄动漫在线观看| 四虎精品免费永久免费视频| 亚洲人成中文字幕在线观看| 午夜无码A级毛片免费视频| 亚洲精品在线免费看| 免费无码成人AV片在线在线播放| 免费视频成人国产精品网站| 黑人精品videos亚洲人| 国内精品免费麻豆网站91麻豆| 激情无码亚洲一区二区三区 | 婷婷久久久亚洲欧洲日产国码AV| 亚洲一区二区三区免费在线观看| 亚洲一卡2卡三卡4卡无卡下载 | 日韩免费高清一级毛片在线| 免费无遮挡无遮羞在线看| 久久久久亚洲av无码尤物| 最近的免费中文字幕视频| 久久国产一片免费观看| 亚洲天堂2017无码中文| 国产亚洲精品线观看动态图| 久久精品国产免费观看| 成人免费网站久久久|