java內部類案例

來源:互聯網
上載者:User

標籤:post   capacity   private   return   dem   contain   進口   ring   i++   

實現索引值對的儲存輸出

import java.util.Arrays;public class EntryDemoTest {//實現索引值對的儲存public static void main(String[] args) {// TODO Auto-generated method stubMyContainer container = new MyContainer();container.put("a", "小明1");container.put("b", "小明2");container.put("c", "小明3");container.put("d", "小明4");MyContainer.Entry[] entrys = container.entryArray();for(int i = 0;i<entrys.length;i++) {MyContainer.Entry entry = entrys[i];System.out.println(entry.getKey()+"----"+entry.getValue());}}}class MyContainer{//定義,初識設定數組容量Entry[] entrys =new Entry[5];private int count = 0;//數組的下標public void put(String key,String value) {//容器的進口Entry entry = new Entry();entry.setKey(key);entry.setValue(value);entrys[count++] = entry;if(count>entrys.length) {int newCapacity;newCapacity = entrys.length*2;entrys = Arrays.copyOf(entrys, newCapacity);}}//返回有資料的內容public Entry[] entryArray() {return Arrays.copyOfRange(entrys, 0 , count);}public static class Entry{//將索引值對封裝在Entry裡,這是個靜態!private String key;private String value;public void setKey(String key) {this.key = key;}public void setValue(String value) {this.value = value;}public String getKey() {return key;}public String getValue() {return value;}}}

  

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.