《Thinking in java》阅读笔记(2)-数组

myhalfsea posted @ 2011年1月02日 23:54 in JAVA , 1161 阅读

先看一段程序:

public class Test {

    public static void main(String[] args) {
        int[] a1 = {1, 2, 3};
        int[] a2 = a1;
        for( int i = 0; i < a1.length; i++ ){
            a2[i] += 1;
            System.out.println("a["+i+"] = "+a1[i]);
        }
    }
    
}

输出为:

a[0] = 2
a[1] = 3
a[2] = 4

程序中a2只是a1的别名,他们指向相同的空间。所有a2的改变,在a1中叶体现。

 

所有数组有一个属性:length,数组下表介于0到length-1之间,超出后,将抛出运行时异常,但在c/c++中,将默默接受,允许方位其内存,最终导致很难检查的错误。

刚看到一段神奇的代码,在之前,从来没见过此种写法:
public class Test {
    
    public void f(Character...a){
        System.out.println("first");
        for( Character c: a){
            System.out.println("a---:"+c);
        }
    }
    public void f(Integer...a){
        System.out.println("second");
        for( Integer c: a){
            System.out.println("b===:"+c);
        }
    }
    public static void main(String[] args) {
        Test t = new Test();
        t.f('a','b', 'c');
        t.f(1, 2, 3, 4);
    }
}

输出:

first
a---:a
a---:b
a---:c
second
b===:1
b===:2
b===:3
b===:4

其实方法定义时,只是将参数列表装换为数组,如果列表中没有任何元素,则转换后的数组长度为0。

疑问:Character也是数据类型? 等同于 char ?

关于Arrays的用法:

Arrays的方法只有几个,Arrays.asList(), Arrays.fill(...),Arrays.binarySearch(...), Arrays.sort(...), Arrays.copyOf(...), Arrays.equeals(...)等等

关于sort(T[] a, Comparator<? super T> c)的使用举例:其实与c差不多

public class TestSort{
    
    public static void main(String[] args) {
        TestSort t = new TestSort();
        Point[] p = new Point[]{new Point(1, "rose"),new Point( 15, "jack"),new Point( 3, "lily"),new Point( 7, "jane")};
        Arrays.sort(p,Comptor.getComparator() );
        for( int i = 0; i < p.length; i++ ){
            System.out.println(p[i].getA()+":"+p[i].getB());
        }
    }
}

class Comptor{
    public static Comparator getComparator() {
         return new Comparator() {
             public int compare(Object o1, Object o2) {
                if( o1 instanceof Point && o2 instanceof Point ){
                    return comp((Point)o1, (Point)o2);
                }
                else return 0;
            }
        };
    }
    public static int comp( Point p1, Point p2 ){
        if( p1.getA() > p2.getA() ) return 1;
        if( p1.getA() == p2.getA() ){
            if( p1.getB().charAt(0) >= p2.getB().charAt(0) ) return 1;
            else return 0;
        }
        else return 0;
    }
}
class Point{
    private int a;
    private String b;
    
    Point(){}
    Point( int a, String b ){
        this.a = a;
        this.b = b;
    }
    public void setA( int a ){
        this.a = a;
    }
    public void setB( String b ){
        this.b = b;
    }
    public int getA(){
        return a;
    }
    public String getB(){
        return b;
    }
}

输出:

1:rose
3:lily
7:jane
15:jack

boardmodelpaper.com 说:
2024年1月27日 15:41

The Board model paper" typically refers to a sample or model question paper that is designed by educational boards or institutions for various exams. These papers serve as practice material for students preparing for exams, providing them with an idea of the question format, difficulty level, and the type of content that may be covered in the actual examination. boardmodelpaper.com Model papers are usually created for specific subjects or courses. They cover a range of topics and chapters that students are expected to have studied during the academic term. Students often use these educational board model papers as an integral part of their exam preparation strategy, helping them familiarize themselves with the exam pattern and refine their understanding of the subject matter.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta
Butterfly Theme | Design: HRS Hersteller of mobile Hundeschule.