java實現無序數組結構

來源:互聯網
上載者:User

標籤:elf   ghost   pack   false   大小   sys   color   遍曆   init   

一、數組的2種定義方式

資料類型 []  數組名稱 = new 資料類型[數組長度];

這裡 [] 可以放在數組名稱的前面,也可以放在數組名稱的後面,一般放在名稱的前面

資料類型 [] 數組名稱 = {數組元素1,數組元素2,......}

這種方式聲明數組的同時直接給定了數組的元素,數組的大小有給定的數組元素個數決定

public class ArrayStruct {    public static void main(String[] args) {//        int[] nums = new int[10];//        int nums[] = new int[10];//        nums = initArray( nums );        //        int[] nums = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };        int nums[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };        printArray( nums );    }        public static int[] initArray( int[] arr ){        for( int i = 0; i < arr.length; i++ ){            arr[i] = i * 10;        }        return arr;    }        public static void printArray( int[] arr ){        for( int i = 0; i < arr.length; i++ ){            System.out.print( arr[i] + "\t" );        }        System.out.println();    }}

 二,實現一個自訂的數組結構,包含以下基本操作:

>插入資料

>刪除資料

>尋找資料

>遍曆資料等

 

package com.ghostwu;class MyDefineArrayStruct {    private int[] arr;    private int curLen;    private int length;        public MyDefineArrayStruct(){        curLen = 0;        length = 30;        arr = new int[length];    }        public MyDefineArrayStruct( int _length ) {        curLen = 0;        length = _length;        arr = new int[length];    }            public int length (){        return curLen;    }        public void print(){        for( int i = 0; i < curLen; i++ ){            System.out.print( arr[i] + "\t" );        }        System.out.println( );    }        public boolean add( int _value ){        if( curLen >= length ){            return false;        }else{            arr[curLen++] = _value;        }        return true;    }        public int getItem( int _index ){        if( _index < 0 || _index > curLen ) {            System.out.println( "數組下標越界" );        }        return arr[_index];    }        public int find( int _value ){        int i;        for( i = 0; i < curLen; i++ ){            if( arr[i] == _value ){                break;            }        }        if( i == curLen ) {            return -1;        }        return i;    }        public boolean delItem( int _value ){        int res = find( _value );        if( res == -1 ) return false;        else {            if( res == curLen - 1 ) {                curLen--;            }else {                for( int i = res; i < curLen - 1; i++ ){                    arr[i] = arr[i+1];                }                curLen--;            }                    }        return true;    }        public boolean updateItem( int _oldValue, int _newValue ){        int res = find( _oldValue );        if( res == -1 ){            System.out.println( "數組中不存在" + _oldValue );            return false;        }else{            arr[res] = _newValue;            return true;        }    }    }public class SelfDefineArrayStruct {    public static void main(String[] args) {                MyDefineArrayStruct arr = new MyDefineArrayStruct( 10 );        arr.print();        arr.add( 10 );        arr.add( 20 );        arr.add( 30 );        arr.add( 40 );                        arr.add( 100 );        arr.print();        arr.delItem( 10 );        arr.print();        System.out.println( arr.length() );        arr.delItem( 20 );        System.out.println( arr.length() );        arr.updateItem( 30, 300 );        arr.updateItem( 40, 400 );        System.out.println( arr.length() );        arr.print();    }}

 

java實現無序數組結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.