【資料結構】數組操作(LowArrayApp.java)

來源:互聯網
上載者:User

標籤:

 1 // LowArrayApp.java   2 // demonstrates array class with low-level interface 3 // to run this program: C>java LowArrayAPP 4 //java資料結構和演算法(第二版)拉佛 著  pdf第46頁  數組操作 5  6 package first; 7  8 class LowArray 9 {10     private long[] a;                 // ref to array a11     12     public LowArray(int size)         // constructor13     { a = new long[size]; }           // create array14     15     public void setElem(int index, long value)      // set value16     { a[index] =  value; }17     18     public long getElem(int index)    // get value19     { return a[index]; }20     21 }    // end class LowArray22 23 24 public class LowArrayAPP 25 {26     public static void main(String[] args)27     {28         LowArray arr;                  // reference29         arr = new LowArray(100);       //create LowArray object30         int nElems = 0;                // number of items in array31         int j;                         // loop variable32         33         arr.setElem(0, 77);34         arr.setElem(1, 99);35         arr.setElem(2, 44);36         arr.setElem(3, 55);37         arr.setElem(4, 22);38         arr.setElem(5, 88);39         arr.setElem(6, 11);40         arr.setElem(7, 00);41         arr.setElem(8, 66);42         arr.setElem(9, 33);43         nElems = 10;        // now 10 items in array44     45     // display items46     for (j = 0; j < nElems; j++) 47         System.out.print(arr.getElem(j) + " ");48         System.out.println("");49     50     //search for data item51     int searchKey = 26;52     for(j=0; j < nElems; j++)53         if(arr.getElem(j) == searchKey)54             break;55     if(j == nElems)56         System.out.println("Can‘t find " + searchKey);57     else58         System.out.println("Found " + searchKey);59     60     //delete value 5561     for(j = 0; j < nElems; j++)62         if (arr.getElem(j) == 55)63             break;64     65     for(int k = j; k < nElems; k++)        // 刪掉的資料項目後面所有單元逐個前移一位66         arr.setElem(k, arr.getElem(k+1));67     nElems--;68     69     // display items70         for (j = 0; j < nElems; j++) 71             System.out.print(arr.getElem(j) + " ");72             System.out.println("");73     74     } // end main()75 } // end class LowArrayAPP 


運行結果:
77 99 44 55 22 88 11 0 66 33 Can‘t find 2677 99 44 22 88 11 0 66 33 

 

 

 

【資料結構】數組操作(LowArrayApp.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.