学习List using Arrays
涉及到字符串(Character String)的比较,先放相关部分的代码
    public boolean insert(T item){
        boolean inserted=false;
        if(!full())
            if (!search(item)){
                for (int j=count;j>position;j--)
                    larray[j]=larray[j-1];
                larray[position]=item;
                count++;
                inserted=true;
            }
        else
                System.out.println("List is Full");
        return  inserted;
    }
    public boolean search(T item){
        boolean found, stop;
        found=false;
        stop=false;
        position=0;
        while (position!=count&&!stop)
            if (larray[position].compareTo(item) >= 0) {
                stop = true;
                if (larray[position].compareTo(item) == 0)
                    found = true;}
            else
                position++;
        //for ordered list; stops exactly at the location where item is found or pointer passes one item after possible location
        return found;
    }
这里compareTo在比较数字大小时候与普通数学运算是一样的,而字符串呢?书上案例涉及一堆字符串放到数组里,于是我对数组里的顺序产生了兴趣。
输入数据是以下opcode:
STORE
ADD
LOAD
WORD
DIV
STOP
SUB
END
START
MUL
经过查询,字符串的比较是先把字符串里所有字符(character)转换成Unicode然后再进行比较。
一开始我以为整个字符串的大小是把所有字符Unicode加起来,经过输出结果的分析,应该是按照字符顺序依次比较,两个字符串如果第一个的首字符Unicode比第二个的首字符小那在(ascending)ordered list里一定会排在第二个的前面,无论后面字符大小。两个字符串首字符相同则从第二个字符开始比较。
所以它们在larry(list array)里的储存状态根据output()得到的是:
ADD DIV END LOAD MUL START STOP STORE SUB WORD 
顺便推荐Guide to Data Structures - A Concise Introduction Using Java (Streib),一本真正适合初学者的书。