Java數組與數據結構

Java編程語言中,把數組作爲對象來看待,因此在創建數組時必須使用new操作符。

Int[] intArray=new int[100];

[]操作符對於編譯器來說是一個標誌,它說明正在命名的是一個數組對象而不是普通的變量。

由於數組是一個對象,所以它的名字是數組的一個引用;它並不是數組本身。數組存儲在內存中的其它地址中,而intArray僅僅保存着這個地址。

數組有一個length字段,通過它可以得知當前數組的大小:

int arrayLength=intArray.length;

跟大多數編程語言一樣,一旦創建數組,數組大小便不可改變。

數組中最基本的操作就是插入、查找、刪除。

public class HighArray {

    private long[] a;

    private int nElems;

   

    /** Creates a new instance of HighArray */

    public HighArray(int max) {

        a=new long[max];

        nElems=0;

    }

//    ...........................................................

    public boolean find(long searchKey)

    {

        int j;

        for(j=0;j<nElems;j++)

            if(a[j]==searchKey)

                break;

        if(j==nElems)

            return false;

        else

            return true;

    }   //end find()

//    ...........................................................

    public void insert(long value){

        a[nElems]=value;

        nElems++;

    }

//    ...........................................................

    public boolean delete(long value)

    {

        int j;

        for(j=0;j<nElems;j++)

            if(value==a[j])

                break;

        if(j==nElems)

            return false;

        else

        {

            for(int k=j;k<nElems;k++)

                a[k]=a[k+1];

            nElems--;

            return true;

        }

    }   //end delete()

//    ...........................................................

    public void display()

    {

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

            System.out.print(a[j]+" ");

        System.out.println("");

    }   //end display()

}

假 設一個數組,其中的數據項按關鍵字升序或降序排列,稱爲有序數組。將數組進行排序的好處是可以通過二分查找顯著地提高查找速度。不好的方面是在插入操作中 由於所有靠後的數據都需移動以騰開空間,所以速度較慢。有序數組和無序數組中的刪除操作都很慢。這是因爲數據項必須向前移動來填補已刪除數據項的洞。

有序數組在查找頻繁的情況下非常有用,但若是插入和刪除較爲頻繁時,則無法高效工作。

public class OrdArray {

    private long[] a;

    private int nElems;

    /** Creates a new instance of OrdArray */

    public OrdArray(int max) {

        a=new long[max];

        nElems=0;

    }

   

    public int size(){

        return nElems;

    }

   

    public int find(long searchKey){

        int lowerBound=0;

        int upperBound=nElems-1;

        int curIn;

       

        while(true){

            curIn=(lowerBound+upperBound)/2;

            if(a[curIn]==searchKey)

                return curIn;

            else if(lowerBound>upperBound)

                return nElems;

            else

            {

                if(a[curIn]<searchKey)

                    lowerBound=curIn+1;

                else

                    upperBound=curIn-1;

            }

        }

    }

   

    public void insert(long value){

        int j;

        for(j=0;j<nElems;j++)

            if(a[j]>value)

                break;

        for(int k=nElems;k>j;k--)

            a[k]=a[k-1];

        a[j]=value;

        nElems++;

    }

   

    public boolean delete(long value){

        int j=find(value);

        if(j==nElems)

            return false;

        else

        {

            for(int k=j;k<nElems;k++)

                a[k]=a[k+1];

            nElems--;

            return true;

        }

    }

   

    public void display()

    {

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

            System.out.print(a[j]+" ");

        System.out.println("");

    } 

   

}

存儲對象

通常我們存儲的數據記錄是許多字段的集合。在java中一條數據記錄經常由一個類對象來表示。例如可以構造一個典型的類來存儲一個公司的職員數據。

public class Person {

    private String lastName;

    private String firstName;

    private int age;

    /** Creates a new instance of Person */

    void Person(String last, String first, int a) {

        lastName=last;

        firstName=first;

        age=a;

    }

    void displayPerson() {

        System.out.print("  Last name: "+lastName);

        System.out.print(", Firstname: "+firstName);

        System.out.print(",  Age: "+age);

    }

    public String getLast() {

        return lastName;

    }

}

構造函數創建一個新的Person對象並將它的各個字段初始化。DisplayPerson方法顯示了一個Person對象的數據。GetLast返回Person的姓;這是用於搜索所需的關鍵字字段。

public class Person {

   

    private String lastName;

   

    private String firstName;

   

    private int age;

   

    /** Creates a new instance of Person */

   

    public Person(String last, String first, int a) {

        lastName=last;

        firstName=first;

        age=a;

    }

   

    public void displayPerson() {

        System.out.print("  Last name: "+lastName);

        System.out.print(", Firstname: "+firstName);

        System.out.println(",  Age: "+age);

    }

   

    public String getLast() {

        return lastName;

    }

   

}

public class ClassDataApp {

   

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        int maxSize=100;

        ClassDataArray arr;

        arr=new ClassDataArray(maxSize);

       

        arr.insert("Evans", "Patty",24);

        arr.insert("Smith","Lorraine",37);

        arr.insert("Yee","Tom", 43);

        arr.insert("Adams", "Henry", 63);

        arr.insert("Hashimoto","Sato",21);

        arr.insert("Stimson","Henry",29);

        arr.insert("Velasquez", "Jose", 72);

        arr.insert("Lamarque","Henry", 54);

        arr.insert("Vang", "Minh",22);

        arr.insert("Creswell","Lucinda",18);

       

        arr.displayA();

       

        String searchKey="Stimson";

        Person found;

        found=arr.find(searchKey);

        if(found!=null)

        {

            System.out.print("Found ");

            found.displayPerson();

        }

        else

            System.out.println("Can't find "+searchKey);

       

        System.out.println("Deleting Smith,Yee,and Creswell");

        arr.delete("Smith");

        arr.delete("Yee");

        arr.delete("creswell");

        arr.displayA();

    }

   

}

Trackback: [url]http://tb.blog.csdn.net/TrackBack.aspx?PostId=64046[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章